Dim
Previous Top Next

Declares regular, array and structure variables.
Syntax: Dim [Type (Fields)] Variable Name [(Array length)] [As Data Type]
Array dimension can be up to three.
You can declare many variables on one row.
Example: Dim Counter(20), I, a As String, x(10,10,10), Matrix(10,10) as Number
Note that the type definition affects all variables preceding the type (and without a type of their own).
In the above example, the variable Counter, I and a are declared as Strings and x and Matrix are Numbers.

When declaring arrays, remember that the array's index starts from 0 and its last index is array length - 1.
In this example, Counter starts from Counter(0) and ends with Counter (19).
Global variables are declared under sub Globals.
Array variables and structures can only be global and therefore must first be declared in sub Globals.

These variables can later be redeclared anywhere in the program.
Array variables and structures can be declared to hold data of a specific data type.
The same array can be declared many times and its size can be changed.
Some methods return arrays.
In this case you can declare the array as an empty array:
Example: Dim buffer(0) As Byte
Now use the declared array to receive the returned array.
Example: buffer() = Serial.InputArray
You can use ArrayLen to find the length (size) of an array.

Declaring structures is similar to declaring regular variables or arrays.
The difference is the fields declaration.
It is done using the Type modifier with the list of fields.
Example: Dim Type(X,Y) point
This statement will declare a structure named point with the fields X and Y.

Example: Dim Type(X,Y) points(50)
This statement will declare an array of structures named points with the fields X and Y.
You can declare arrays of structures with one or two dimension.

See Module topic for more information about global variables access modifiers.