Declaring array variables

There are several different ways of declaring array variables. The first way is to use empty parentheses and explicitly specify the type of the array:

'Declare x to be a Global variable
'of Number Array type
Global x () As Number 
'Initialize x
x = Array (10, 20, 30)
'Declare y to be a Shared variable
'of String Range Array type
Shared y () As String Range
'Initialize y
y = Array ("A" To "C", "H" To "J")

The second way is to declare the variable without specifying that it is an array and without giving its type and waiting for the first assignment to the variable to completely specify its type:

'Declare y to be a Local variable
'but do not specify its type
Dim y
'The type of y is now set to be a String Array
y = Array ("Sun", "Mon", "Tue", "Wed", "Th", _
           "Fri", "Sat")

The third way is to declare that the variable is an array but not specify its type fully until the first assignment. Assuming the declaration of y above:

'Declare z to be a Local variable that is an Array
Local z()
'z is set to Array ("Mon", "Tue") and is a String Array
z = y(2 to 3)

The fourth way is to explicitly specify the size of the array during the declaration. If you use this technique, the array is automatically created and default values are used to fill the array. For example, for a Number Array, each element is initialized to 0 and for a String array each element is initialized to the empty string "". Since this type of declaration actually creates the array, you must specify its type with the As clause so that Seagate Crystal Reports knows how much storage space to reserve for the array.

Dim a(2) As String
'Assign a value to the first element of the array a
a(1) = "good"
a(2) = "bye"

'The & operator can be used to concatenate strings

'the formula returns the String "goodbye"
formula = a(1) & a(2)
Arrays and For/Next loops

Arrays are commonly used with For/Next loops. The following example creates and then uses the array Array (10, 20, 30, ..., 100) using a For/Next loop. See For/Next loops for more details.

Dim b (10) As Number
Dim i
For i = 1 To 10
   b(i) = 10 * i
Next i
formula = b(2) 'The formula returns the Number 20

Several variables can be declared in a single statement by separating their declarations with commas:



Seagate Software, Inc.
http://www.seagatesoftware.com
Please send comments to:
techpubs@seagatesoftware.com