lang • arraydecl

Array Declaration

DIM Identifier AS [ NEW ] Datatype [ Array dimensions ... ]

Note that you can use any expression for specifying array dimensions.


Dim aWords As New String[WORD_MAX * 2]
Dim aMatrix As New Float[3, 3]
Dim aResult As String[]

In Gambas 3, any datatype can be used as array element.


Dim aLabel As New Label[12, 12]
Dim aResult As New String[][12] ' An array of string arrays!


The array can have several dimensions, up to a maxium of eight.


Dim iGroupc As New Integer[27, 9]
Dim iFieldr As New Integer[9]
Dim iX9X As New Integer[3, 4, 5, 2, 3, 2, 2, 4, 2] 'will report error

The name "DIM" for this declaration comes from the sixties, where BASIC variables did not need to be declared, except variables with dimensions.

Gambas uses brackets [ ] instead of braces ( ) to declare and use dimensions.

Embedded arrays

[ STATIC ] { PUBLIC | PRIVATE } Identifier [ Array dimensions ... ] AS Native Datatype

An embedded array is an array that is allocated directly inside the object where it is declared.

Such an array cannot be shared, and is destroyed with the object.

An embedded array cannot be public, and you cannot initialize it.

Embedded arrays were created to ease the interface between Gambas and external functions located in shared libraries.

Consequently, I strongly suggest to use them only if you cannot use normal arrays.

Or if your code run faster with embedded arrays than with normal arrays, as it's possible in some cases.

In Gambas 3, embedded arrays cannot be used as local variables anymore. But they can be public!

Since 3.17

Embedded array dimensions can be defined with constant expressions.


Private Handles[8] As Label
Static Private TicTacToe[3, 3] As Integer

See also