8 Global Variables

A variable declaration consists of the keyword variable, the data type, the variable identifier and the terminator ;. In one declaration more than one variables of the same data type can be defined. In this case the identifiers of the variables have to be separated by , (commas).

If a variable is declared and not within a rule or method (see also chapter “Local Variables”), it is called a global variable.

In IDM global variables are treated like objects so that not only the content of a variable but the variable itself can be changed at runtime (see chapter “Attributes of Global Variables that Can Be Changed Dynamically”).

8.1 Variable Definition

Syntax

{ export | reexport } variable <data type> <variable name>
  [ , <variable name> ] ;

The following data types are available:

Data type

Value Range

anyvalue

Arbitrary, undefined data type; will be defined only by actual occupation.

attribute

Attribute data type in the IDM.

boolean

Boolean value (true | false).

datatype

IDM data type.

class

Class of an object, e.g. "window".

enum

Symbolic constant; enumeration type for special attributes, e.g. "sel_row".

event

Type of event, e.g. "select".

index

Index value for 2-dimensional attributes [I,J].

integer

Integral number ("long integer"), e.g. 42.

method

Method, e.g. "delete".

object

Object in the sense of the IDM (actual objects, resources, functions, rules,…).

pointer

Pointer from the application.

string

Character string which can be arbitrarily long (zero terminated), e.g. "hello".

void

Data type without value.

8.2 Variable Definition with Initialization

The following definition initializes the generated variable immediately with a value. The data type of the value has to be identical with the data type of the variable.

Syntax

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

Object references, numbers, colors, strings, and boolean values, i.e. anything which is supported by the ISA Dialog Manager, can be stored in variables of the type anyvalue.

Example

variable integer Filling_level;

variable boolean Valve_state := true;

variable string  Valve_identifier := "main valve";

variable enum    Mode;

variable object  PbSwitch;  // Let PbSwitch be a pushbutton

A character string is always in quotation marks.

Valve_identifier := "main valve";

In this example the words "main valve" are a string.

8.3 Configurable Variables

The additional keyword config marks a global variable as configurable, i.e. its value can be set in a configuration file.

Syntax

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

The configuration file is loaded with the functions loadprofile(), DM_LoadProfile or DMcob_LoadProfile.

See Also

Attribute .configurable

Chapter “Configuration File” in manual “Development Environment”

8.4 Protecting Variables against Changes

Changes to the content of a global variable can be prevented by using the keyword constant instead of variable in the declaration or by setting the attribute .constant of the variable to true.

Syntax

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

See Also

Attribute .constant

Example

dialog D

 

variable integer V := 123;

constant integer C := 456;

 

on dialog start

{

  C:=V;      // Evaluation error because C cannot be changed

  V := 234;  // OK

  V.constant := true;

  V := 345;  // Error – Variable is write-protected now

}

8.5 Attributes of Global Variables that Can Be Changed Dynamically

The table below shows those attributes of variables, that can be queried and changed dynamically at runtime.

Attribute

Value Range

Meaning

.value

anyvalue

Value

.type

datatype

Data type

.constant

boolean

Changeability of content

In order to refer to the variable itself and not to its content the attribute .self is used. This is followed by the attribute that shall be addressed.

Example

variable integer I := 2;

 

print I.self.type    // integer

print I.self.value   // 2

 

I.self.type := boolean;

print I.self.type;  // boolean

Note

This applies to global variables only and not to local variables (i.e. variables declared within rules and methods).