It’s no secret that you have to define variables in a block in order to assign values to them, but how do you go about doing that?

There are two definitive ways to define variables, you can either define them all upfront, or define them as you go.

Upfront Definition

This is the most obvious and most often used approach. Using the Declaration Area at the top of a Program Block object, the variable and data type is added.

The variable can then be used in the project as normal:

Example of Upfront Definition

In the example above, the data was added into the declaration area (the table at the top of the screen), and then used on Network 1‘s instructions.

Because the data already existed in the declaration area, TIA Portal can display it using a form of Intellisense. This is where TIA Portal will recommend the variable to use based on what you’ve typed in so far and what the Data Type is:

Example of TIA Portal providing a recommendation

This is useful if you know exactly what you want in your program block, but quite a lot of the time variables may be unknown, or you are creating a specific function and need to “declare as you go”

Declaring Variables On-The-Fly

Another approach is to declare variables “on-the-fly”, or as you need them. This is an approach made popular by other development environments such as Codesys, which prompts you to declare a variable that is not recognised.

In TIA Portal, an unrecognised variable is given a red underline:

Undeclared and unrecognised variables

IN3, IN4 and OUT2 are not recognised in the above example. In order for TIA to recognise them, they must be declared.

This can be done by simply typing them in the declaration window, or by using the Define tool:

Right-Click –> Define Tag

By Right-Clicking on the variable you wish to define, and choosing the Define tag option, the following window pops up:

Define tag dialogue

Using this window, you can name the tag (which is prefilled for you), set the data type (which is also done for you) and select the Section of the declaration table in which the variable will be inserted. The Section is always set to Local Temp. Using the dropdown, this can be changed to the type you need.

Note – Sometimes Global Memory is automatically selected for the Section, it depends on how you’ve named your variable.

You can also add a comment.

Defining Multiple Tags

You can take this a step further too! You can actually define multiple unrecognised variables at once:

[membership level=”-4″]

⚠️ Membership Required

[pmpro_checkout_button class=”pmpro_btn” level=”4″ text=”Become A Lifetime Gold Member”]

🔒What Am I Missing Here?

An example of how to define multiple tags at once in network-based languages

[/membership]

[membership level=”4″]

Defining 3 variables at once

This is done by Right-clicking blank space on the network and choosing Define Tag.

This time, all unrecognised variables are populated in the Define Tag dialogue window. From here you can set them all up with their required settings, speeding up the process.

Note – This only works for network-based languages, for a text-based language, only singular variables can be defined.

[/membership]


Check Out Another Post

Leave a Reply