Project members: Array

The member array is intended to store state information within a component, similarly to a variable, but unlike variables, one array can manage multiple values (items) at the same time. Every array has an explicitly specified data type (e.g. string, color, bool, ...) determining the possible content of the array and optional expressions to individually initialize the array items. Arrays are represented in Composer usually by following bricks:

An array can be defined within a class or class variant only. If one class is derived from another class, all arrays defined in the ancestor classes are inherited implicitly. In the derived class the inherited arrays can be overridden individually with new initialization expressions for their items.

In order to preserve the clarity, all arrays inherited from a base class but not overridden in the actual class are not shown as bricks in the Composer. The Inspector, in turn, shows always the complete list including all inherited members. If you are looking for a particular inherited array, use the Inspector for this purpose. Please note, inherited but not overridden arrays are shown in Inspector with gray thin font:

Inherited but not overridden arrays appear only in the Inspector window. They are shown with thin gray font.

If an inherited array has been overridden, Inspector shows it with dark gray font. Additionally, overridden arrays appear as bricks in the Composer window. You can distinguish easily, whether an array is overridden or whether it is new by considering the font color in its brick. New arrays are shown with black font. Overridden arrays, in turn, with gray font:

Overridden arrays appear in Inspector with dark gray font. They are also shown as bricks in Composer, again with gray font.

Arrays are ideal to store runtime state information. If there are several instances of one and the same class existing, every instance will manage its own copy of arrays. In object-oriented sense, arrays are the attributes or the fields of an object.

Add new array

First switch to the Composer page for the respective class or class variant definition, where you want to add the new array.

Then, in the Gallery window, ensure that the folder Chora is opened.

Look in the folder for the template named Array.

With the mouse, select the template and drag it into the Composer.

Drop the template within the Composer.

Adding a new array member.

Name the array

First ensure, that the array member is selected.

Press the key F2 or select the menu item EDITRename ....

Enter the new name in the Inspector window.

Arrays have a local character - they can be accessed only in context of an instance of the class, in which they are defined. To address an array, you use its name. Please note, the name of an inherited array can't be changed retrospectively in the derived class.

Determine the data type of the array

Every array expects the data type to be specified explicitly in its attribute Type. Accordingly, the array items can store values matching this type declaration only. Please note, new added arrays are configured per default with the data type int32. Moreover, the data type of an inherited array can't be changed retrospectively in the derived class.

Determine the size of the array

Every array expects the size (the capacity) to be specified explicitly in its attribute Dimension. The size determines how many items the array can store. Please note, new added arrays are configured per default with 4 items. Moreover, the size of an inherited array can't be changed retrospectively in the derived class. Also the size can't change dynamically at the runtime of the application.

Determine the initialization values for array items

With the attribute Items you can specify expressions to initialize the items of an array - individually for every item. If an expressions is omitted, the corresponding item will retain its original value. This means in particular for new added arrays, that the item retains a zero value corresponding to the data type of the array (an empty string, a transparent color, boolean value false, ...). Please note, the resulting data type of an expression must match the type declaration of the array.

Control the initialization order

At the runtime, when new instance of a class is created, all items of arrays defined within the class are initialized automatically with their corresponding initialization expressions. This initialization is performed strictly in the order in which the members are defined within the class, starting with the back most member (the member with the lowest order number).

The actual order of a member can be verified in the members area of the Inspector window. To change the initialization order you need to explicitly restack the member. This, however, is necessary only in cases, when one array item is initialized with expression containing as operand another member. This 'operand' member, in such case, has to be initialized first.

Localization

At the runtime, when the language is changed, Embedded Wizard automatically re-evaluates all initialization expressions, which involve multi-lingual operands. For example, if an array item is initialized with a localized constant, the initialization expression is re-evaluated and the resulting, eventually different constant value is re-assigned to the array item automatically. See also the section Managing localization.

Duplicate an existing array

You can create any number of copies of an already existing array member.

First ensure, that the array member is selected.

Press the keys CtrlC and CtrlV in succession ...

... or hold the key Ctrl pressed while you drag and drop the selected array member.

Finally rename the just duplicated array member.

Override an inherited array

When an item of an inherited array is modified, the array will automatically become overridden causing its brick to appear in Composer. The Inspector shows overridden arrays with dark gray instead of thin font as explained above. You override an array if you want the array items to be initialized with expressions different to those specified in its inherited version.

Revert to inherited array value

If you want to revert an overridden array to its original inherited state, delete it simply. Thereupon, its brick disappears from the Composer and the Inspector shows the variable name with thin font again. In turn, to revert a single item to its inherited value, select the item and press the keys CtrlR.

Use the array

The array items can be evaluated within Chora expressions wherever an operand with the data type matching the array's declaration is expected. Similarly, the items can modified individually in assignment operations. To address an item, you use the name of the array followed by the index of the desired item.

In case of one-dimensional arrays, the index must be an expression resulting in an unsigned integer value and it must be enclosed between a pair of [...] (squared brackets). Arrays are always used in scope of the object, the array belongs to. If no scope is specified, the operation is considered as being performed in context of this object. For example:

var int32 inx; // Increment all items within the array 'numbers' of 'this' object for ( inx = 0; inx < numbers.size; inx = inx + 1 ) numbers[ inx ] = numbers[ inx ] + 1; // Increment all items within the array 'numbers' of a foreign object for ( inx = 0; inx < someObject.numbers.size; inx = inx + 1 ) someObject.numbers[ inx ] = someObject.numbers[ inx ] + 1;

In case of a multidimensional array, the index must be a , (comma) separated list of unsigned integer expressions enclosed between a pair of [...] (squared brackets). The number of expressions must correspond to the specified dimension of the array. For example:

var int32 col; var int32 row; // Assuming, there is a two-dimension array 'tiles' defined with // its Dimension configured as '10, 20', then the following loop // will iterate through all items of the array and initialize // them with random values: for ( row = 0; row < 10; row = row + 1 ) for ( col = 0; col < 20; col = col + 1 ) tiles[ row, col ] = math_rand( 0, 100 );

Please note, to obtain the size of an array, you can use the size instant property.

Control the code generation

With the attribute Generator you can selectively control, whether the array member should be taken in account during the code generation or not. Configuring this attribute with the value false will exclude the member from your project unless the member name is explicitly involved within a Chora expression.

Delete an array

First ensure, that the array member is selected.

Press the key DEL or select the menu item EDITDelete.

Please note, inherited arrays can't be deleted. However, if an array is overridden you can delete this overriding.

Attributes of an array

The following table shows the complete list of attributes provided by the array member:

Attribute name

Short description

Brick

Determines the position and the size of an array member brick within the Composer window.

Description

Contains the description for the array member.

Dimension

Determines the number of items within an array member. Please note, this attribute can't be modified if the array is inherited.

Generator

Controls the code generation for the affected array member.

Items

Determines the initial values for array items.

Type

Determines the data type of an array member. Please note, this attribute can't be modified if the array is inherited.

Chora syntax of an array

This section describes the Chora syntax of how arrays are stored inside Embedded Wizard project files. The knowledge about the syntax can be helpful when you plan to process the project contents by third party tools. In particular, the general purpose Export and Import functionality introduced in version 14 requires a good understanding of how project members are represented in Chora syntax to correctly interpret the exported members and synthesize the Chora code for members to import. Please note, Embedded Wizard project files (and thus Chora code) are encoded as text in UTF-8 format.

In Chora syntax, array definition is always introduced with the keyword array, followed by the data type, the name, the size of the array and optionally initialization values for array items. The size of the array (number of array items) is enclosed between a pair of [..] square brackets. The data type and the size correspond to contents of the attributes Type and Dimension. The complete array definition terminates with ; semicolon sign. Wherever necessary, the tokens are separated by whitespace signs.

If item initialization values are specified, the name and the values are separated by an = assignment operator and the values are enclosed between a pair of (..) parenthesis signs. Each initialization value consists of the name Default followed by the index of the affected item enclosed between [..] square brackets, an = assignment operator and the value. Each initialization ends with with ; semicolon sign. The specified initialization values have to match the data type of the array. The initialization values corresponds to the content of the attribute Items.

The definition of an array can be initiated by a comment block providing optional description for the array. The content of this comment corresponds to the value of the array's attribute Description. Furthermore the directives $rect and $output can be specified providing values for the corresponding attributes Brick and Generator.

In case of an array inherited from a base class, the array definition is additionally prefixed by the keyword inherited. In this case the array inherits the data type, the size of the array and the value for the $output directive from the base class. Modifying these values in the inherited array is not foreseen. The following examples demonstrate the described syntax:

// This is an array containing 4 'int32' items. Since there are no \ // values assigned to the array, all items are zero-initialized. // // $rect determines the position of the brick inside Composer window. $rect <10,10,210,50> array int32 Counters[ 4 ]; // This is an array intended to store references to objects. The item \ // 1 and 2 of the array are initialized with references to some objects \ // 'Text' and 'Image'. // // $rect determines the position of the brick inside Composer window. // $output imposes the code generation of the array regardless of its \ // usage inside the project. $rect <10,10,210,50> $output true array object Views[ 10 ] = ( Default[1] = Text; Default[2] = Image; ); // This is an array inherited from a base class. Its data type and size \ // are determined in the base class and can't be changed in descending \ // classes. The item 5 in the array is re-initialized with a value 'Password'. // // $rect determines the position of the brick inside Composer window. $rect <10,10,210,50> inherited array Labels[] = ( Default[5] = "Password"; )

Each Chora file (or text exchanged with an external Export and/or Import tool) can be (or even should be) prefixed with $version directive. This directive determines the version number of the included Chora code. For details concerning the syntax of the version please see the chapter $version macro. Furthermore, the code can include a $scope directive followed by a path to a project member acting as the original owner of the corresponding code. That means the members described by the Chora code existed inside the member specified in $scope. In case of an array, $scope would specify the name of the class the array belongs to.