Crystal Reports Online Training

Learn Online, Anytime, Anywhere

Step-by-step online tutorials.

B. 3 Arrays and Ranges

Array Data Types

To declare an array variable in Basic syntax, use a set of parentheses in the variable declaration.

Dim X() As String

To reference an element of the array, use parentheses with the index number.

X(1) = 5
Formula = X(3)

The ReDim statement changes the number of elements in the array and resets their values. If you want to keep the existing values intact, then also use the Preserve keyword.

Redim var(number) ‘Redimension array and reset all values
Redim Preserve var(number) ‘Preserve existing values

Assigning values to an array is done in different ways. If you know what the values of an array are during the development process, you can initialize the array with these values using the Array() function. Pass the Array() function all the elements as a comma delimited list and it returns an array that is fully populated with these elements. When using the Array() function you don’t have to specify the array size. Crystal Reports figures that out for you.

As expected, if you want to simply assign a value to an individual element in the array, then the index must be within the array’s bounds. But if you are assigning an entire array to another array variable and they are different sizes, you do not have to redimension the target array. The target array gets overwritten and it will have the size of the existing array.

‘Demonstrate initializing an array and then overwriting it
Dim MonthsInSeason() As String
MonthsInSeason = Array(“May”, “June”, “July”)
If LongSummer = True Then
MonthsInSeason = Array(“May”, “June”, “July”, “August”)
End If

Range Data Types

To declare a variable as a Range data type, declare it as one of the standard data types and put the Range keyword at the end of the declaration. The data types that are allowed to have a related Range data type are Number, Currency, String, DateTime, Date, and Time.

Dim var As datatype Range
Dim X As Number Range