Project members: Variable

The member variable is intended to store state information within a component. Every variable has an explicitly specified data type (e.g. string, color, bool, ...) determining the possible content of the variable and an optional expression to initialize it. Variables are represented in Composer usually by following bricks:

A variable can be defined within a class or class variant only. If one class is derived from another class, all variables defined in the ancestor classes are inherited implicitly. In the derived class the inherited variables can be overridden individually with new initialization expressions.

In order to preserve the clarity, all variables inherit 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 variable, use the Inspector for this purpose. Please note, inherited but not overridden variables are shown in Inspector with gray thin font:

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

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

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

Variables 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 variables. In object-oriented sense, variables are the attributes or the fields of an object.

Add new variable

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

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

Look in the folder for the template named Variable.

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

Drop the template within the Composer.

Adding a new variable member.

Name the variable

First ensure, that the variable member is selected.

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

Enter the new name in the Inspector window.

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

Determine the data type of the variable

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

Determine the initialization value of the variable

The variable can be configured with an optional initialization expression specified in its attribute Default. If the expression is omitted for a new added variable, the variable retains its data type specific zero value (an empty string, a transparent color, boolean value false, ...). Please note, the resulting data type of the expression must match the type declaration of the variable.

Control the initialization order

At the runtime, when new instance of a class is created, all variables 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 variable is initialized with expression containing as operand another member. This 'operand' member, in such case, has to be initialized first.

Localization

With the attribute MultiLingual you can configure a class to handle as far as possible the language selection by itself. If this attribute is true, Embedded Wizard will automatically re-evaluate all initialization expressions, which involve multi-lingual operands. For example, if a variable is initialized with a localized constant, the initialization expression is re-evaluated and the resulting, eventually different constant value is re-assigned to the variable automatically.

Duplicate an existing variable

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

First ensure, that the variable member is selected.

Press the keys CtrlC and CtrlV in succession ...

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

Finally rename the just duplicated variable member.

Override an inherited variable

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

Revert to inherited version

If you want to revert an overridden variable 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.

Use the variable

Once defined, the name of the variable can be used within Chora expressions wherever an operand with the data type matching the variable's declaration is expected. Moreover, a new value can be assigned to a variable. Variables are always used in scope of the object, the variable belongs to. If no scope is specified, the operation is considered as being performed in context of this object. For example:

// Increment a variable 'tapCounter' in context of 'this' object
tapCounter = tapCounter + 1;

// Increment a variable 'tapCounter' in context of a foreign object
someObject.tapCounter = someObject.tapCounter + 1;

Control the code generation

With the attribute Generator you can selectively control, whether the variable 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 a variable

First ensure, that the variable member is selected.

Press the key DEL or select the menu item EDITDelete.

Please note, inherited variables can't be deleted. However, if a variable is overridden you can delete this overriding.

Attributes of a variable

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

Attribute name

Short description

Brick

Determines the position and the size of a variable member brick within the Composer window.

Default

Determines the initial value of a variable member.

Description

Contains the description for the variable member.

Generator

Controls the code generation for the affected variable member.

Type

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