10.10 Local Variables

Local variables can be used to store any value within a rule. They are only known in the rule in which they were defined. This is where they differ from global variables, which are known in the entire dialog or module (see chapter “Global Variables”).

Within the rule where they are defined, local variables overlay any existing global variables with the same identifier.

Local variables can be declared as normal variables or as static variables:

10.10.1 Normal Local Variables

Syntax

variable <data type> <variable name> { := <expression> }
                 [ , <variable name> { := <expression> } ] ;

Several variables of the same data type can be declared in one statement. The identifiers of the variables are separated by , (comma).

Local variables can be initialized with a value or an expression directly in the definition. If an expression is used for initialization, the following should be noted:

Example

rule integer R1(integer X)
{
  variable integer Z := X * 3;

  if (X > 100) then
    Z := Z / 2;
  endif

  return Z;
}

A definition without direct initialization is also possible. In this case, however, a value must have been assigned before the first read access to the variable.

Example

rule void R2 ()
{
  variable integer MyNumber;

  MyNumber := 5;

  print MyNumber;
}

Once local variables contain a value, they can be assigned to attributes of objects. It only needs to be ensured that the data types of the local variable and the attribute allow this (identical data types or object attribute of type anyvalue).

Example

rule void R3 ()
{
  variable string MyWord;

  MyWord := "Hello world";
  Edittext.content := MyWord;
}

A variable of the type object can be used like an object, i.e. every attribute of the object stored in the variable can be changed.

Example

rule void R4 ()
{
  variable object MyObject;

  MyObject := MyWindow;
  MyObject.title := "New Title";
  MyObject.visible := true;
}

10.10.2 Static Variables

Static variables are declared by the additional keyword static before the keyword variable. Unlike normal local variables, they can only be initialized with values or other variables, but not with expressions.

Syntax

static variable <data type> <variable name> { := <value> | <variable> }
                        [ , <variable name> { := <value> | <variable> } ] ;

Static variables may have the same data types as global or local variables.

The initializations in the declarations of static variables are only carried out in the first rule call, but not in further calls. This also applies if the value of the variable used for initialization has changed in the meantime. Instead, their values from the last rule execution remain unchanged.

To initialize static variables, only variables that were previously defined in the rule code or as global variables in the dialog or module may be used.

Examples

variable integer G:=10;
rule Example (object O input)
{
  variable boolean B;
  static variable integer X:=1, Y:=G, Z:=Y;
  static variable object  Q:=O;
  static variable boolean J:=B;
  variable integer I:=X;
}
rule Example2
{
  static variable anyvalue A;
  if A = 1 then
    print "something";
  else
    A := 1;
    print "further initializations";
  endif
}