User Tools

Site Tools


k7:k7.4:k7.4.3:k7.4.3.2:start

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:

  • A public variable aKL for the derived array of array type CDS[] is initialized in line 1. KL could, for example, stand for' ClassList'.
  • All elements of the one-dimensional arrays aKL and myKL are' CDS' objects.
  • A new' CDS' object of the class CDS is created in line 7. The data for the new record (JGS, BirthDate and surname) is then defined and inserted into the array myKL created in line 5. The same occurs for the second data record in lines 13 to 17.
  • The function returns the derived array myKL as a function value.
The website uses a temporary session cookie. This technically necessary cookie is deleted when the browser is closed. You can find information on cookies in our privacy policy.
k7/k7.4/k7.4.3/k7.4.3.2/start.txt · Last modified: 05.02.2022 (external edit)

Page Tools