Working with variables

A variable is a container for concrete values. If you need to carry out calculations or store intermediate results for a longer period, you can define a variable which will take the required value. The value can then be read out from the variable and used at any time.

The content of a variable can also be changed at any time by assigning it a different value.

Variables always have a certain type. This means, for instance, that a variable of the type string can only store strings. Similarly, a variable of the type floating comma can only store decimal numbers and so on.

Definition

A variable must be defined before you can use it. This means you have to create the variable and you have to specify what sort of elements it can store, for instance strings or culture-specific information. In addition, a variable has to have a name:

1 A) integer a;

2 integer b;

3 integer c;

4

5 B) a = 5;

6 b = 7;

7 C) c = a + b;

8

9 D) Trace(TRACELEVEL_INFO, Format("Content of a: {0}", a));

10 Trace(TRACELEVEL_INFO, Format("Content of b: {0}", b));

11 Trace(TRACELEVEL_INFO, Format("Content of c: {0}", c));

Notes:

A) This and the following two lines define three variables with the name a, b and c. All three variables have the type integer. This means that they can only store whole numbers.

B) This line changes the content of the variable a to the value 5. The sign = signifies an assignment. The variable on the left of the sign is assigned the value on the right of the sign.

C) Here, a and b are added together by using the + operator The result of the addition is then assigned to the variable c.

D) As a check, the current values of the three variables are output here. The result is as follows:

Content of a: 5

Content of b: 7

Content of c: 12

The construction c = a + b is not a mathematical equation. The = sign means that the variable on the left of the sign is assigned the value that results from the calculation on the right of the sign.

Initializing

The three variables in the above examples are only defined, not initialized. A non-initialized variable does not contain a concrete value, and if it is read out before a value is assigned to it, then this causes an error.

However, you can also initialize a variable when you define it, i.e. you assign it with an initial value. This means that the variable does not have to be set separately. Initialization once again uses the assignment sign:

1 integer a = 5;

2 integer b = 7;

3 integer c = a + b;

4

5 Trace(TRACELEVEL_INFO, Format("Content of a: {0}", a));

6 Trace(TRACELEVEL_INFO, Format("Content of b: {0}", b));

7 Trace(TRACELEVEL_INFO, Format("Content of c: {0}", c));

The result is the same as the one in chapter "Definition" above.

Constants

As the name says, variables are variable. A variable can be assigned a different value at any time. However, this is not always desirable because these assignments can sometimes happen inadvertently.

To prevent a variable from having other values assigned to it, you can specify them as a constant:

const integer var = 1234;

The keyword const means that the variable can not be assigned a different content.

Since constants can not be assigned values at a later point, they must be initialized in the definition.

Persistent variables

The functions ReadPersistVars() and WritePersistVars() can be used to store the contents of variables at a particular point and then to read them out again. This means that variables can be used irrespective of the runtime of a task.

1 String myName = "Adam";

2 Integer myNumber = 15;

3

4 ReadPersistVars("persistentVariables", ["myName", "myNumber"]);

5 WritePersistVars("persistentVariables", ["myName", "myNumber"]);