Tinderbox v9 Icon


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]


 9.0.0, 9.1.0

var vName

var vName(value)

var:data-type vName

var:data-type 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.

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.

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 'vNum'.

More sensible is to use a name indicating purpose:

var vNum; declares a variable 'vNum'.

var vNum(5); declares a variable 'vNum' and gives it an initial value of 5.

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

Local variables may be initialised by assignment as well as declaration.

var vCost = 5; 

defines a temporary variable 'vCost' and assigns it the value '5'. It is equivalent to the original syntax:

var vCost(5); 

In effect, it collapses the older two-step method of declaring an empty var before giving it a value:

var vCost; 


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.

Setting a data type

From v9.1.0. vars can optionally be given a data type on creation, by colon-appending the data-type to the var operator. Thus:

var:number vCost(5); 

This generates a variable 'vCost' of (expected ) data type Number and sets an initial value of '5'.

Available data types include "number", "set", "list", "date", "color", "date", "interval", and "dictionary". Notre that data type labels are all-lowercase. Providing an explicit type helps Tinderbox to understand the user's intent. For example, the same numerical value number might react to subsequent operations in various ways:

var:number vCost(5); vCost = vCost + 5; $MyString= vCost; gives 10

var:list vCost((5); vCost =vCost + 5; $MyString = vCost; gives 5;5

var:string x(5); vCost =vCost + 5; $MyString = vCost; gives 55

Further usage Examples:

$MyNumber = 0; var vNum = 2; $MyNumber = vNum; 

$MyNumber = 0; var vNum; vNum = 2; $MyNumber = vNum; 

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

In all cases $MyNumber is set to 2. In the second example the variable is defined and given a value using a single code expression. Here is a scoped example within a single action:

	$MyNumber = 0; {
		var vNum = 2;
		$MyNumber = vNum;
		vNum = 6;
	$MyNumber = vNum;


$MyNumber is set to 0 (nothing) as 'vNum' has no meaning outside the code inside the braces {}.

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

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


$MyNumber is 4, rather than 0, because the vNum = 4 is interpreted as the deprecated legacy syntax of $vNum = 4. So, be careful that a local variable's names does not match existing attributes, or string literals that you use in your project.

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.