Values for array elements
General information
PowerBuilder initializes each element of an array to the same
default value as its underlying datatype. For example, in a newly
declared integer array:
|
1 |
integer li_TaxCode[3] |
the elements li_TaxCode[1], li_TaxCode[2], and li_TaxCode[3] are
all initialized to zero.
For information about default values for basic datatypes, see
Initial values for
variables.
Simple array
In a simple array, you can override the default values by
initializing the elements of the array when you declare the array. You
specify the values in a comma-separated list of values enclosed in
braces. You do not have to initialize all the elements of the array, but
you cannot initialize values in the middle or end without initializing
the first elements.
Multidimensional array
In a multidimensional array, you still provide the values in a
simple, comma-separated list. When the values are assigned to array
positions, the first dimension is the fastest-varying dimension, and the
last dimension is the slowest-varying. In other words, the values are
assigned to array positions by looping over all the values of the first
dimension for each value of the second dimension, then looping over all
the values of the second dimension for each value of the third, and so
on.
Assigning values
You can assign values to an array after declaring it using the
same syntax of a list of values within braces:
|
1 2 |
integer li_Arr[] Li_Arr = {1, 2, 3, 4} |
Examples
Example 1
This statement declares an initialized one-dimensional array of
three variables:
|
1 |
real lr_Rate[3]={1.20, 2.40, 4.80} |
Example 2
This statement initializes a two-dimensional array:
|
1 |
integer li_units[3,4] = {1,2,3, 1,2,3, 1,2,3, 1,2,3} |
As a result:
Li_units[1,1], [1,2], [1,3], and [1,4] are all 1
Li_units[2,1], [2,2], [2,3], and [2,4] are all 2
Li_units[3,1], [3,2], [3,3], and [3,4] are all 3
Example 3
This statement initializes the first half of a 3-dimensional
array:
|
1 2 |
integer li_units[3,4,2] = & {1,2,3, 1,2,3, 1,2,3, 1,2,3} |
As a result:
Li_units[1,1,1], [1,2,1], [1,3,1], and [1,4,1] are all 1
Li_units[2,1,1], [2,2,1], [2,3,1], and [2,4,1] are all 2
Li_units[3,1,1], [3,2,1], [3,3,1], and [3,4,1] are all 3
Li_units[1,1,2], [1,2,2], [1,3,2], and [1,4,2] are all 0
Li_units[2,1,2], [2,2,2], [2,3,2], and [2,4,2] are all 0
Li_units[3,1,2], [3,2,2], [3,3,2], and [3,4,2] are all 0