-

Arrays

Arrays are declared in the data type worksheet using a TYPE...END_TYPE declaration block. The array may be multi-dimensional. Array data types consist of several elements of the same data type. Each element can be accessed via its unique element index.

Note
(Nested) Arrays can also be defined on the 'Variables' and 'Data List' tables in the 'Type' column using the following syntax:
 
ARRAY_<lower bound>_<upper_bound>_OF_<type>.
 
The type for the array will be automatically created invisibly. The array data type is not available on a datatype worksheet.
 
Examples:
ARRAY_0_9_OF_INT (1-dimensional array)
ARRAY_0_9_OF_ARRAY_0_5_OF_BYTE (nested array)

Array size (boundaries) definition with literals and constant variables

The array size (number of elements or values the array can hold) can be defined using either literals (fixed values) or global or local constants. A global constant is created by selecting the 'Constant' attribute for a global variable defined in the variables table/data list. Local constants are declared as VAR CONSTANT in the data type worksheet using a VAR CONSTANT...END_VAR declaration block (see the following example). The initialization value of a constant variable must be a literal (expressions for the initialization value are not supported).

Note
Local constants can only be used in the same data type worksheet.

Examples

Example: 1-dimensional array of 10 integers; array size defined using literals

Data type definition
Variable declaration
('Variables'/Data List)
Use in code

Example: 1-dimensional array of 10 integers; array size defined using constant variables

Data type definition
Variable declaration
('Variables'/Data List)
Use in code

Example: 2-dimensional array; declaration using an array of an already declared array

Data type definition
Variable declaration
('Variables'/Data List)
Use in code

Further Info
See also the topic "User-defined data types".

Initializing arrays

Arrays, or to be more precise, the individual elements contained in an array, are initialized using the following syntax. The array initializer is a list of comma-separated literals (fixed values) or local or global constants (see above) enclosed by square brackets.

For initializing arrays with a large number of values, so-called repetition numbers can be used.
Example of an initialization in a data types worksheet:

This initialization means the same as:

In the 'Init Value Configuration' editor, these repetition numbers can be entered as initialization statement to initialize arrays.

Note
If you are using PLCnext Engineer in another user interface language and/or with another Windows system locale than English you must enter numeric values with a '.' as decimal separator (always according to the English standard). This applies, for example, when entering initial values in variables tables or REAL/LREAL literals in the code.