Visibility of variables

This section really belongs with chapter "Working with variables", where working with variables was discussed. However, since you needed to know about loops and alternatives first, we have moved it to the end of the chapter.

A variable is always valid for a certain area. This is the area where it is known and where it can be used.

The examples described in this chapter are also available as the predefined project Structure on the TDM Studio Start Page.

Project variables

If a variable is defined in the project variables, then it is known throughout the project and it can therefore be used and changed everywhere. Let us assume that the project variables contain the following definition:

integer projectVariable = 0;

In addition, there should be two templates in the project, named Change project.tpl and Display project.tpl.

Change project.tpl has the following content:

projectVariable = 10;

And Display project.tpl has this content:

Trace(TRACELEVEL_INFO, projectVariable);

There is also a task in which these templates are used. The task has the name Project variables. The templates can be added to the task with the mouse, in the order shown in the screenshot below:

Task “Project variables"

If you now run this task, it generates to following output:

0

10

The first template to be executed is Display project.tpl. Since the project variable has not been changed anywhere yet, it still has its initial value of 0, which is output first.

Next, the template Change project.tpl is executed. Here, projectVariable is set to the value 10.

Finally, Display project.tpl is run again and it outputs the new variable content 10.

This illustrates how project variables, once their value is set, keep this value across all templates and tasks.

Task variables

If a variable is defined in the task variables, then it is known for all the templates in the task. However, if a template is also assigned to a different task in which the variable is not defined, then it is not known to the template there.

If the value of this variable is changed in a template, then this change only applies to the task in which the template is currently run.

If you define a variable in the task variables with the same name as a variable in the project variables, then the variable in the task variables takes precedence before the variable in the project variables. This means that a template in the task will access the task variable and not the variable defined in the project variables.

You should therefore take care when naming variables in order to avoid unexpected results. On the other hand, you can exploit exactly this behavior, for instance to create a special initialization for a particular task, while all other tasks use the project variables.

Variables in templates

Variables that are defined in a template are only known within this template. They can not be used or changed in a different template.

If you define a variable in a template with the same name as a variable in the project or task variables, then the variable in the template takes precedence before the variable in the project or task variables. This means that the template will access the template variable and not the variable defined in the project or task variables. You should therefore take care when naming variables in order to avoid unexpected results.

On the other hand, you can exploit exactly this behavior, for instance to create a special initialization for a particular template, while all other templates use the project or task variables.

Variables in blocks

A block is a collection of statements within a branch of an alternative or within a loop.

If variables are defined within a block, then they are only known for this block and all the blocks contained within the block:

1 for integer i = 0 to 5 do

2 string place;

3 ...

4 place = "Room 13"; // OK, place is known

5 ...

6 if i == 5 then

7 integer value = -1;

8 place = Str(value); // OK, value and place are known

9 endif

10 ...

11 value = 9; // Error, value not known

12 endif

13

14 place = "Room 4"; // Error, place not known

If you define a variable in a block with the same name as a variable in the template or in the project or task variables, then the variable in the block takes precedence before the variable in the project, task or template variables.

This means that a statement in the block will access the block variable and not the variable defined in the project, task or template variables. You should therefore take care when naming variables in order to avoid unexpected results.

Changing the visibility

You can explicitly change the visibility of a variable. This can be useful in situations where you don't want to use particular project or task variables in templates (e.g. because they only store intermediate results):

1 private float pi2 = 2 * PI;

2

3 float pi2Sin = Sin(pi2);

4 float pi2Log = Log(pi2);

If these lines are entered into the project or task variables, then pi2Sin and pi2Log can be used in the templates. However, pi2 can not be used, because it is marked as private. This means that it is only known within the project or task variable that contains its definition.