This version is out of date, covering development from v8.0.0 to v8.x.x. It is maintained here only for inbound reference links from elsewhere. It is no longer actively updated.

Jump to the current version of aTbRef

Tinderbox v8 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

var vName(value)

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 initial 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:

$MyNumber = 0; var x; x = 2; $MyNumber = x; 

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

In both cases $MyNumber 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:

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

$MyNumber 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:

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

$MyNumber 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 will not be mistaken for something else. Avoid using a 'var' prefix for variable names, such as 'varX', as this will confuse Tinderbox's parser. In such a case 'vX' might be a better choice.

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

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