7.4.3.2 Derived arrays

The class of derived arrays is created' on the fly' by the interpreter. Three examples of derived arrays:

Dim aLabels As New Label[]
Dim aComponents As New Component[]

Dim KL As New CDS[] ' Array of the declared class CDS

Example 1 - Using a One-Dimensional Derived Array

All components loaded in the current project are stored in a derived, one-dimensional array, immediately read out and displayed:

Source code extract:

[1] Dim hComponent As Component
[2] Dim aComponents As New Component[]
[3] Dim iCount As Integer
[4]
[5] For Each hComponent In Components ' Iterate over all loaded classes ...
[6]   aComponents.Add(hComponent)
[7] Next
[8]
[9] For Each hComponent In aComponents
[10]   Inc iCount
[11]   Print "Componentes  "; icount; "  =  "; hComponent.Name
[12] Next

Output in the IDE console:

Component  1  =  gb.debug
Component  2  =  gb.image
Component  3  =  gb.qt4
Component  4  =  gb.form
Component  5  =  gb.qt4.ext
Component  6  =  gb.eval
Component  7  =  gb.draw
Component  8  =  gb.geom
Component  9  =  gb.gui.base

Example 2 - Using a Derived Array

In contrast to the first example, a self-written class CDS is used. The array class CDS[] does not actually exist. However, since it ends with “[]” and there is a class CDS, the interpreter automatically creates a CDS[] derived from CDS, which represents a one-dimensional array of' CDS' objects.

Source code CDS.class

' Gambas class file
' This class is a (pure) data structure without its own methods.
 
Public JGS As Integer
Public BirthDate As Date
Public Lastname As String

Source code FMain.class

[1] Public aKL As New CDS[]
[2]
[3] Public Function FillClassArray() As CDS[]
[4]   Dim hCDS As CDS
[5]   Dim myKL As New CDS[]
[6]
[7]   hCDS = New CDS
[8]     hCDS.JGS = 12
[9]     hCDS.BirthDate = Date(2004, 5, 19)
[10]     hCDS.Lastname = "Mayer"
[11]   myKL.Add(hCDS)
[12]
[13]   hCDS = New CDS
[14]     hCDS.JGS = 11
[15]     hCDS.BirthDate = Date(2005, 12, 19)
[16]     hCDS.Lastname = "Eagle"
[17]   myKL.Add(hCDS)
[18]
[19]   Return myKL
[20]
[21] End ' Function()

Comments: