Dim X(d1{,d2,d3,...}) {=1,2,3,4,...}

The Dim statement declares a variable as an array and allocates storage for each element of the array. Each value in parentheses specifies the maximum index value of each given dimension.

An array may have any number of dimensions. The total size of an array is limited to 2^31-1 total elements but you will run out of memory before you get to that size.

Since array subscripts may range from 0 up to and including the dimension specified in the Dim statement, d1+1 elements are allocated.

The total number of elements in an array can be calculated by multiplying the size of each dimension.

Dim A(10,10,10)

total size = 11 * 11 * 11 = 1331

A Dim statement may be followed by initializers. As in:

Dim A(10) = 0,1,2,3,4,5,6,7,8,9,10

String initializers must be quoted.

Dim B$(2) = "one", "two", "three"

A Dim statement may also be initialized with an array constant.

dim a(2,1) = [[1,2,3],[4,5,6]]

Array Constants

Only one array can be dimensioned per Dim statement.

An array may be dimensioned as many times as you like. Each time a Dim statement is executed the array is reinitialized. To change an array's dimensions without loosing the contents of an array, use the ReDim command.


Arrays and Subscripts

Become a Patron