# GAMBAS BOOK 3.19.3

## Data types

### Chapters

08.10.2022
k7:k7.4:k7.4.2:start

## 7.4.2 Dynamic arrays

A dynamic array is an array that is a real Gambas object. The size of a dynamic array can be queried at runtime. A dynamic array with a dimension can be resized at any time.

Example:

```[1] Public Sub btnShowExample_Click()
[2]   Dim i, j As Integer
[3]   Dim a2DDateArray As New Date[][]
[4]   Dim myArray As Date[]
[5]
[6]   a2DDateArray.Resize(10) ' *
[7]
[8]   For i = 0 To 9
[9]     a2DDateArray[i] = New Date[]
[10]     a2DDateArray[i].Resize(5) ' **
[11]     For j = 0 To 4
[12]         a2DDateArray[i][j] = Date(Now() + j – i) ' Generate different date values
[13]     Next ' j
[14]   Next ' i
[15]
[16] ' Output IDE console (example schedule):
[17]   For Each myArray In a2DDateArray
[18]     For j = 0 To myArray.Max
[19]       Print Format(myArray[j], "dd. mm. yyyy"); " | ";
[20]       If (j + 1) Mod (myArray.Max + 1) = 0 Then Print
[21]     Next ' j
[22]   Next ' myArray
[23]
[24]   Print "Dimension of a2DDateArray: ", a2DDateArray.Dim
[25]   Print "Number of elements of 'a2DDateArray': ", a2DDateArray.Count
[26]   Print "Number of elements of 'a2DDateArray': ", a2DDateArray.Bounds[0] ' Alternative
[27]
[28] End ' btnShowExample_Click()```
• An empty array for date values' a2DDateArray' of type Date[] is created in row 3.
• In line 6, space is also dynamically reserved for 10 elements of the data type Date[].
• In the two FOR-NEXT control structures (lines 8 to 14), Date[] arrays are created (line 9), the number of elements in the Date[] array is set to 5 with the resize (…) method? and each filled with 5 different date values.
• In lines 17 to 22, each element is read from the array' a2DDateArray' and then the 5 date values stored in each element are read and grouped together. This is ensured by lines 19 and 20.

Output in the IDE console:

```29. 04. 2014 | 30. 04. 2014 | 01. 05. 2014 | 02. 05. 2014 | 03. 05. 2014 |
28. 04. 2014 | 29. 04. 2014 | 30. 04. 2014 | 01. 05. 2014 | 02. 05. 2014 |
27. 04. 2014 | 28. 04. 2014 | 29. 04. 2014 | 30. 04. 2014 | 01. 05. 2014 |
26. 04. 2014 | 27. 04. 2014 | 28. 04. 2014 | 29. 04. 2014 | 30. 04. 2014 |
25. 04. 2014 | 26. 04. 2014 | 27. 04. 2014 | 28. 04. 2014 | 29. 04. 2014 |
24. 04. 2014 | 25. 04. 2014 | 26. 04. 2014 | 27. 04. 2014 | 28. 04. 2014 |
23. 04. 2014 | 24. 04. 2014 | 25. 04. 2014 | 26. 04. 2014 | 27. 04. 2014 |
22. 04. 2014 | 23. 04. 2014 | 24. 04. 2014 | 25. 04. 2014 | 26. 04. 2014 |
21. 04. 2014 | 22. 04. 2014 | 23. 04. 2014 | 24. 04. 2014 | 25. 04. 2014 |
20. 04. 2014 | 21. 04. 2014 | 22. 04. 2014 | 23. 04. 2014 | 24. 04. 2014 |

Dimension of 'a2DDateArray':           1

Number of elementes of 'a2DDateArray':     10
Number of elementes of 'a2DDateArray':     10 ' Alternative```

The structure of the grouped display also corresponds to the memory structure in the array' a2DDateArray', as you can access the 4th date in the 7th element, for example:

`Print Format(a2DDateArray[6][3], "dd. mm yyyy")`

Note that both 'a2DDateArray' and all arrays 'a2DDateArray[i]' (–> line 9) with the index i from the interval from 0 to 9 are one-dimensional arrays and you therefore have the possibility to change the dimension at runtime! In the lines (1*) and (2*) this is also practiced, which would not be possible with the following (static) declaration:

`Dim a2DDateArray As New Date[10][5]`

You could do without the help array' myArray' if you change the source code for the output in this way:

```  For i = 0 To a2DDateArray.Max
For j = 0 To a2DDateArray[i].Max
If (j + 1) Mod (a2DDateArray[i].Max + 1) = 0 Then
Print Format(a2DDateArray[i][j], "dd. mm. yyyy"); " | "
Else
Print Format(a2DDateArray[i][j], "dd. mm. yyyy"); " | ";
Endif
Next ' j
Next ' i```

Dynamic arrays of structures are currently not supported (–> chapter 7.2.1.2 Arrays of structures), because' Struct' is meant to be a structured data container. If you need dynamic arrays, use a class instead of a structure, because in Gambas you can derive a dynamic array type from each class.

Structure:

```Public Struct Schueler
JGS As Integer
BirthDate As Date
Lastname As String
DG1Course As String
DG2Course As String
End Struct```

Source code CDS. class:

```' Gambas class file

' This class is a (pure) data structure without its own methods.
' For example, it represents a data record

Public JGS As Integer
Public BirthDate As Date
Public Lastname As String
Public DG1Course As String
Public DG2Course As String```

In the main program, you can use CDS[] to generate a dynamic array type whose elements are objects of type CDS –> Chapter 7.4.3.2 Derived arrays. You can work with this derived array in the same way as you are used to working with structures, but without the (static) constraints of a structure.

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.