Crystal Reports  

Declaring Array Variables (Crystal Syntax)

You can declare array variables by following the type name with the keyword Array.

Example

//Declare x to be a Global variable of
//Number Array type
Global NumberVar Array x := [10 , 20, 30];
//cost is a Global variable of Currency Array type
//It is automatically Global since the scope specifier
//(one of Local, Global or Shared) is omitted.
CurrencyVar Array cost := [$19.95, $79.50, $110.00,
                           $44.79, $223.99];
//payDays is a Global variable of Date Array type
Global DateVar Array payDays := [CDate(1999, 5, 15),
CDate(1999, 5, 31)];
//y is a Shared variable of String Range Array type
Shared StringVar Range Array y := ["A" To "C",
                                   "H" To "J"];
//days is a Local variable of String Array type
Local StringVar Array days;
days := ["Sun", "Mon", "Tue", "Wed", "Th",
         "Fri", "Sat"];

Assigning Values to Elements of an Array

You can assign values to elements of an array and also use the values of the elements for other computations.

Example

StringVar Array x := ["hello", "bye", "again"];
x [2] := "once"; //Now x is ["hello", "once", "again"]
//The expression below would cause an error if not
//commented out since the array has size 3
//x [4] := "zap";
//The formula returns the String "HELLO"
UpperCase (x [1])

The Redim and Redim Preserve keywords can be used to resize an array if you want to add extra information to it. Redim erases the previous contents of the array first before resizing it whereas Redim Preserve preserves the previous contents.

Local NumberVar Array x;
Redim x [2]; //Now x is [0, 0]
x [2] := 20; //Now x is [0, 20]
Redim x [3]; //Now x is [0, 0, 0]
x [3] := 30; //Now x is [0, 0, 30]
Redim Preserve x [4]; //Now x is [0, 0, 30, 0]
"finished"
Local StringVar Array a;
Redim a [2];
//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
a[1] & a[2] //The formula returns the String "goodbye"

Arrays and For Loops

Arrays are commonly used with For loops. The following example creates and then uses the Array [10, 20, 30, ..., 100] using a For loop.

Local NumberVar Array b;
Redim b[10];
Local NumberVar i;
For i := 1 To 10 Do
(
   b[i] := 10 * i
);
b [2] //The formula returns the Number 20

See Also

For Loops