Tinderbox 7 Icon

var


Operator Type: 

Operator Scope of Action: 

Operator Purpose: 

Operator First Added: 

Operator Altered: 

 Property   [other Property type actions]

 Item   [operators of similar scope]

 Data manipulation   [other Data manipulation operators]

 Baseline

 


var()

Occasionally, an action may find it convenient to declare a local variable in which to save intermediate results. In the past, the only choice was to use a user attribute as a temporary store, with the attendant issues of deciding if it is necessary to reset the attribute at the end of the expression.

A local variable acts in most ways like a user attribute. Local variables exist for the duration of the action or, when they are declared inside curly brackets { … } their scope is the rest of the clause - i.e the remaining individual statements within the {}. The var() statement declares the local variable:

var x - declares a variable 'x'.

var YY(5) - declares a variable 'YY' and gives it an ititial value of 5.

var myZ("this is note "+$Name) - declares a variable 'myZ' and give it a calculated string value.

Local variables must be declared before first use. If not explicitly initialised, their initial value is the empty string "". Within scope, a variable maybe reset in the same manner as an attribute. Thus, YY=; resets variable 'YY' to no value.

Usage Examples:

$MyNum = 0; var x; x = 2; $MyNum = x; 

$MyNum = 0; var x(2); $MyNum = x; 

In both cases $MyNum is now 2. In the second example the variable is defined and given a value using a single code expression. Now, a scoped example within a single action:

$MyNum = 0; {var x; x = 2; $MyNum = x; x=6;};$MyNum = x; 

$MyNum is now 0 (nothing) as 'x' has no meaning outside the code in {}.

Beware name collisions. Assume for a moment you have an actual attribute $x, then:

$MyNum = 0; {var x(2); $MyNum = x; x = 6;}; x = 4; $MyNum = x; 

$MyNum is 4, rather than 0, because the x=4 is interpreted as the deprecated legacy syntax of $x=4. So, be careful that a local variable's names does not match existing attributes, or string literals that you use in your project. Try to use variable names that won't be mistaken for something else.

Remember the local variable does not take a $ prefix. It is a value reference - but not to an attribute.

Avoid using a 'var' prefix, as in 'varX' as this will confuse Tinderbox's parser. In such a case 'vX' might be a better choice.

A variable declared using var() may be altered from within the scope of a List/Set.each() loop.