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 .
★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
At the runtime, when the language is changed, Embedded Wizard automatically re-evaluates all initialization expressions, which involve multi-lingual operands. For example, if the variable is initialized with a localized constant, the initialization expression is re-evaluated and the resulting, eventually different constant value is re-asigned to the variable automatically. See also the section Managing localization.
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 .
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 |
---|---|
Determines the position and the size of a variable member brick within the Composer window. |
|
Determines the initial value of a variable member. |
|
Contains the description for the variable member. |
|
Controls the code generation for the affected variable member. |
|
Determines the data type of a variable member. Please note, this attribute can't be modified if the variable is inherited. |
Chora syntax of a variable
This section describes the Chora syntax of how variables 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, variable definition is always introduced with the keyword var, followed by the data type, the name and optionally the initialization value of the variable. If the value is omitted, the variable is zero-initialized. If value is specified, the name and the value are separated by an = assignment operator. The complete variable definition terminates with ; semicolon sign. Wherever necessary, the tokens are separated by whitespace signs. The specified initialization value has to match the data type of the variable. The data type corresponds to the content of the attribute Type. The value corresponds to the content of the attribute Default.
The definition of a variable can be initiated by a comment block providing optional description for the variable. The content of this comment corresponds to the value of the variable's attribute Description. Furthermore the directives $rect and $output can be specified providing values for the corresponding attributes Brick and Generator.
In case of a variable inherited from a base class, the var definition is additionally prefixed by the keyword inherited. In this case the variable inherits the data type and the value for the $output directive from the base class. Modifying these values in the inherited variable is not foreseen. The following examples demonstrate the described syntax:
// This is a variable containing a 'int32' value. Since there is no \ // value assigned to the variable, the variable is zero-initialized. // // $rect determines the position of the brick inside Composer window. $rect <10,10,210,50> var int32 Counter; // This is a variable intended to store a reference to a 'bool' property. \ // The variable is initialized with a reference to the property 'Visible' \ // of the object 'Text'. // // $rect determines the position of the brick inside Composer window. // $output imposes the code generation of the variable regardless of its \ // usage inside the project. $rect <10,10,210,50> $output true var ^bool outlet = ^Text.Visible; // This is a variable inherited from a base class. Its data type is \ // determined in the base class and can't be changed in descending \ // classes. The variable is re-initialized with a value 'false'. // // $rect determines the position of the brick inside Composer window. $rect <10,10,210,50> inherited var Selected = false;
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 a variable, $scope would specify the name of the class the variable belongs to.