All the elements in an array have the same data type. BlueZone Basic supports arrays of bytes, Booleans, longs, integers,
singles, double, strings, variants and user-defined types.
You can declare a fixed-size array with one of the following options:
|
• |
To create a global array, use the Dim statement outside the procedure section of a code module to declare the array.
|
|
• |
To create a local array, use the Dim statement inside a procedure.
|
BlueZone Basic supports Dynamic arrays.
To declare an array, the array name must be followed by the upper bound in parentheses. The upper bound must be an integer.
For example:
Dim ArrayName (10) As Integer
Dim Sum (20) As Double
To create a global array, use
Dim outside the procedure. For example:
Dim Counters (12) As Integer
Dim Sums (26) As Double
Sub Main () …
The same declarations within a procedure use
Static or
Dim. For example:
Static Counters (12) As Integer
Static Sums (22) As Double
The first declaration creates an array with 11 elements and with index numbers running from 0 to 10. The second creates an
array with 21 elements. To change the default lower bound to 1, place an
Option Base statement in the Declarations section of a module:
Option Base 1
Another way to specify the lower bound is to provide it explicitly (as an integer, in the range -32,768 to 32,767) using the
To keyword:
Dim Counters (1 To 13) As Integer
Dim Sums (100 To 126) As String
In the preceding declarations, the index numbers of Counters run from 1 to 13 and the index numbers of Sums run from 100 to
126.
Note
Many other versions of Basic allow you to use an array without first declaring it. BlueZone Basic does not allow this; you
must declare an array before using it.
Loops often provide an efficient way to manipulate arrays. For example, the following For loop initializes all elements in
the array to 5:
Static Counters (1 To 20) As Integer
Dim I As Integer
For I = 1 To 20
Counter ( I ) = 5
Next I
…
Multi-dimensional arrays
BlueZone Basic supports multidimensional arrays. For example the following example declares a two-dimensional array within
a procedure:
Static Mat(20, 20) As Double
Either or both dimensions can be declared with explicit lower bounds:
Static Mat(1 to 10, 1 to 10) As Double
You can efficiently process a multidimensional array with the use of For loops. In the following statements the elements in
a multi-dimensional array are set to a value:
Dim L As Integer, J As Integer
Static TestArray(1 To 10, 1 to 10) As Double
For L = 1 to 10
For J = 1 to 10
TestArray(L,J) = I * 10 + J
Next J
Next L
Arrays can be more than two-dimensional. BlueZone Basic does not have an arbitrary upper bound on array dimensions:
Dim ArrTest(5, 3, 2)
This declaration creates an array that has three dimensions with sizes 6 by 4 by 3 unless Option Base 1 is set previously
in the code. The use of Option Base 1 sets the lower bound of all arrays to 1 instead of 0.