Crystal Reports Designer  

宣告陣列變數 (Crystal 語法)

您可以在型別名稱後面接著用關鍵字 Array,來宣告陣列變數。

範例

//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"];

指派數值給陣列的元素

您可以指派陣列元素的值,也可以用這些值來進行其他計算。

範例

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])

如果您想要在陣列中加入更多的資訊,可以使用 Redim 和 Redim Preserve 關鍵字來調整陣列的大小。Redim 會在調整陣列的大小之前,先清除陣列中原來的內容,而 Redim Preserve 則會保存原來的內容。

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"

陣列和 For 迴圈

陣列經常和 For 迴圈一起使用。以下的範例將利用 For 迴圈來建立及使用陣列 [10, 20, 30, ..., 100]。

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

請參閱

For 迴圈