Description
A type that can hold either a number, a string, a treenode or
an Array.
The Variant class is an essential type by which data is exchanged between various
functionalities in FlexSim. As such, the Variant represents a "jack of all trades" that can
hold several different types of data. It can be a number type, holding a number value. It can
be a string type, holding text data. It can be a treenode
type, holding a reference to a node in FlexSim's tree. It can be an Array type,
holding an array of Variants. And finally, it can be a null variant meaning it is none of the other types.
Method/Property Forwarding
Since the Variant is just a flexible container for other data types, its methods and properties
are just forwarders to the various methods and properties of the types that the Variant holds. For
example, if you have a Variant holding a treenode, you can use all of the methods
and properties on a treenode directly on the Variant without having to cast to a treenode with the
as() keyword.
Variant node = param(1);
node.name = "Object1";
node.value = 5;
The only property that is exclusive to the Variant is type,
which tells you what type of data is held by a Variant. All other properties and methods simply forward
to the associated method or property of the held class. If the Variant does not hold the associated type, then
calling the type's method will result in an exception. For example, if a Variant has a number, and you call
the find() method on it, FlexScript will throw an exception.
Casting
Variant will cast to any of its target types without throwing an exception. If the Variant does not
hold the target type, it will just give a null-or-similar result. This means a NULL treenode, empty string,
empty Array, or 0.0 number.
Variant num = 1;
string asString = num; // empty string
Variant str = "Hello";
double asNum = str; // 0.0
To determine the type of data that is held by a Variant, use the
type property, or use the is() keyword.
Variants as Arrays
The Variant implements special logic associated with arrays in that you can treat a Variant like an
Array even if it does not hold array data. If a Variant holds, for example,
a string, you can treat the Variant as if it were a 1-length Array with a
string as its first element.
Variant val = "Hello";
int len = val.length; // len = 1
return val[len]; // returns "Hello";
Also, if you cast a Variant into a non-array type, and the Variant is in fact an Array, the
cast operation will return the first element of the array. Thus you can even treat an Array
Variant as a non-array type.
Variant myArray = ["Hello", "World"];
string myString = myArray;
return myString; // returns "Hello"
These capabilities are especially useful in Process Flow, where you are often dealing with
label values that may or may not contain array data, but you want to interface with those
data in a consistent manner.
Null Values
To check if a Variant is null you can compare the value with the special keyword nullvar or compare
its type with the VAR_TYPE_NULL macro.
Variant val;
val == nullvar //true
val.type == VAR_TYPE_NULL // true
Where this gets more complicated is when dealing with treenode type Variants returned by other properies. See the discussion of treenode null values.
Other data types do not have null types but have an empty type that corresponds to that type:
string emptyString = "";
int emptyInt = 0;
double emptyDouble = 0.0;
Array emptyArray = [];
Properties
Methods
append |
Appends another Variant to this Variant. |
byteToCharIndex |
Returns the index of a given byte in a string Variant. |
charAt |
Returns the character at a given byte index in a string Variant. |
charToByteIndex |
Returns the byte index associated with a specific character index in a string Variant. |
clone |
Clones a Variant. |
concat |
Concatenates two Variants into an Array. |
destroy |
Destroys a treenode Variant. |
endsWith |
Queries whether a string Variant ends with a defined string. |
evaluate |
Evaluates a treenode Variant |
fill |
Fills an Array Variant with a defined value. |
find |
Finds a node in a treenode Variant's sub-tree by path. |
getPath |
Gets the path of a treenode Variant. |
includes |
Queries whether a string Variant includes a string. |
indexOf |
Gets the index of an element in an Array Variant. |
join |
Joins an Array Variant into a string. |
match |
Matches a reqular expression on a string Variant. |
padEnd |
Pads the end of a string Variant with the padString to the given length. |
padStart |
Pads the start of a string Variant with the padString to the given length. |
pop |
Pops an element from an Array Variant. |
push |
Pushes a value onto an Array Variant. |
repeat |
Repeats a string Variant a number of times. |
replace |
Replaces text in a string Variant |
reverse |
Reverses an Array Variant |
search |
Searches a string Variant for an internal string or pattern. |
shift |
Shifts (pops) an element from the front of an Array Variant. |
slice |
Returns a new Array made up of a subsection of an Array Variant. |
splice |
Removes and/or inserts elements into an Array Variant. |
split |
Splits a string Variant into an Array. |
startsWith |
Queries if a string Variant starts with a defined string. |
substr |
Extracts a section of a string Variant and returns a new string. |
toLowerCase |
Creates a lower case string from a string Variant. |
toNum |
Interprets a string Variant's content as a floating point number. |
toUpperCase |
Creates an upper case string from a string Variant. |
trim |
Trims white space off of the ends of a string Variant. |
unshift |
Unshifts (pushes) a value onto the front of an Array Variant. |
Operators
- |
Subtracts two numbers. |
-- |
Decrements a number Variant. |
! |
Tests if a Variant is either a null Variant, a NULL treenode, or a 0.0 number. |
!= |
Tests if a Variant is not equal to another value. |
% |
Returns the modulus of two numbers. |
* |
Multiplies two numbers. |
/ |
Divides two numbers. |
[] |
Accesses an element of an Array Variant. |
+ |
Adds two numbers. |
++ |
Increments a number Variant |
+= |
Increments a number Variant by a defined value, or appends a string to the current string Variant. |
< |
Tests if a Variant is less than a number. |
<= |
Tests if a Variant is less than or equal to a number. |
-= |
Decrements a number Variant by a defined value. |
== |
Tests if two values are equal to each other. |
> |
Tests if a Variant is greater than a number. |
>= |
Tests if a Variant is greater than or equal to a number. |
Details
Do no remove, this fixes the anchor on doc.flexsim.com
int dataType
Description
Gets/sets the dataType property on a treenode Variant.
An exception is thrown if the Variant is not a treenode type.
Do no remove, this fixes the anchor on doc.flexsim.com
treenode first
Description
Gets the first subnode of a treenode Variant.
Forwards to treenode.first.
An exception is thrown if the Variant is not a treenode type.
Do no remove, this fixes the anchor on doc.flexsim.com
readonly labels
Description
Accesses the labels of a treenode Variant.
Forwards to treenode.labels.
An exception is thrown if the Variant is not a treenode type.
Do no remove, this fixes the anchor on doc.flexsim.com
treenode last
Description
Gets the last subnode of a treenode Variant.
Forwards to treenode.last.
An exception is thrown if the Variant is not a treenode type.
Do no remove, this fixes the anchor on doc.flexsim.com
int length
Description
Gets/sets the Array length of a Variant.
If the Variant is already an Array type, this will forward to
Array.length. If the Variant
is a null Variant, length will return 0. If it is a number, string,
or treenode type, length will return 1.
Variant myVar; // initialized as null Variant
myVar.length; // returns 0
myVar = 5; // set to number type
myVar.length; // returns 1
If length is set, the Variant will convert itself into an Array, and forward to
Array.length
Variant myArray = "Hello";
myArray.length = 2; // converts to Array and sets Array length to 2.
Do no remove, this fixes the anchor on doc.flexsim.com
string name
Description
Gets/sets the name of a treenode Variant.
Forwards to treenode.name.
An exception is thrown if the Variant is not a treenode type.
Do no remove, this fixes the anchor on doc.flexsim.com
readonly treenode next
Description
Gets the next node of a treenode Variant.
Forwards to treenode.next.
An exception is thrown if the Variant is not a treenode type.
Do no remove, this fixes the anchor on doc.flexsim.com
readonly treenode prev
Description
Gets the next node of a treenode Variant.
Forwards to treenode.prev.
An exception is thrown if the Variant is not a treenode type.
Do no remove, this fixes the anchor on doc.flexsim.com
int rank
Description
Gets/sets the rank of a treenode Variant.
Forwards to treenode.rank.
An exception is thrown if the Variant is not a treenode type.
Do no remove, this fixes the anchor on doc.flexsim.com
readonly subnodes
Description
Accesses the subnodes of a treenode Variant.
Forwards to treenode.subnodes.
An exception is thrown if the Variant is not a treenode type.
Do no remove, this fixes the anchor on doc.flexsim.com
readonly int type
Description
Gets a Variant's type, one of VAR_TYPE_NUMBER, VAR_TYPE_STRING, VAR_TYPE_NODE, or VAR_TYPE_ARRAY
Do no remove, this fixes the anchor on doc.flexsim.com
treenode up
Description
Gets/sets the parent of a treenode Variant.
Forwards to treenode.up.
An exception is thrown if the Variant is not a treenode type.
Do no remove, this fixes the anchor on doc.flexsim.com
Variant value
Description
Gets/sets the data value of a treenode Variant.
Forwards to treenode.value.
An exception is thrown if the Variant is not a treenode type.
Do no remove, this fixes the anchor on doc.flexsim.com
Parameters
val |
The value to append to this Variant. |
Returns
Description
Appends another Variant to this Variant.
If this Variant is already an Array, this forwards to Array.append().
If the Variant is a null Variant, it will become val. If it is a non-array type, it will convert itself into an Array, and forward to
Array.append().
Do no remove, this fixes the anchor on doc.flexsim.com
Variant.byteToCharIndex()
int byteToCharIndex(
int byteIndex
)
|
Parameters
byteIndex |
The 1-based byte index of the target character. |
Returns
int
|
The 1-based character index of the character. |
Description
Returns the index of a given byte in a string Variant.
Forwards to
string.
byteToCharIndex.
An exception is thrown if the Variant is not a string type.
Do no remove, this fixes the anchor on doc.flexsim.com
Parameters
byteIndex |
The 1-based byte index of the target character. |
Returns
string
|
The target character, as a string. |
Description
Returns the character at a given byte index in a string Variant.
Forwards to
string.
charAt.
An exception is thrown if the Variant is not a string type.
Do no remove, this fixes the anchor on doc.flexsim.com
Variant.charToByteIndex()
int charToByteIndex(
int charIndex
)
|
Parameters
charIndex |
The 1-based character index of the target character. |
Returns
int
|
The 1-based byte index of the character. |
Description
Returns the byte index associated with a specific character index in a string Variant.
Forwards to
string.
charToByteIndex.
An exception is thrown if the Variant is not a string type.
Do no remove, this fixes the anchor on doc.flexsim.com
Returns
Description
Clones a Variant.
If the Variant is an Array type, this forwards to Array.clone().
Otherwise it just returns a copy of its value.
Do no remove, this fixes the anchor on doc.flexsim.com
Parameters
other |
The Array to concatenate onto the end of this Variant. |
Returns
Array
|
The resulting Array. |
Description
Concatenates two Variants into an Array.
Forwards to Array.concat().
Do no remove, this fixes the anchor on doc.flexsim.com
Description
Destroys a treenode Variant.
Forwards to treenode.destroy().
An exception is thrown if the Variant is not a treenode type.
Do no remove, this fixes the anchor on doc.flexsim.com
Description
Queries whether a string Variant ends with a defined string.
Forwards to string.endsWith().
An exception is thrown if the Variant is not a string type.
Do no remove, this fixes the anchor on doc.flexsim.com
Parameters
p1-p20 |
The parameters to pass to the evaluation. |
Description
Evaluates a treenode Variant
Forwards to treenode.evaluate().
An exception is thrown if the Variant is not a treenode type.
Do no remove, this fixes the anchor on doc.flexsim.com
Description
Fills an Array Variant with a defined value.
Forwards to Array.fill().
If the Variant is not already an Array, it simply sets its value to the passed value.
Do no remove, this fixes the anchor on doc.flexsim.com
Description
Finds a node in a treenode Variant's sub-tree by path.
Forwards to treenode.find().
An exception is thrown if the Variant is not a treenode type.
Do no remove, this fixes the anchor on doc.flexsim.com
Description
Gets the path of a treenode Variant.
Forwards to treenode.getPath().
An exception is thrown if the Variant is not a treenode type.
Do no remove, this fixes the anchor on doc.flexsim.com
Description
Queries whether a string Variant includes a string.
Forwards to string.includes().
An exception is thrown if the Variant is not a string type.
Do no remove, this fixes the anchor on doc.flexsim.com
Description
Gets the index of an element in an Array Variant.
Forwards to Array.indexOf().
If the Variant is not an Array, it is treated as an array of length 1.
Do no remove, this fixes the anchor on doc.flexsim.com
Description
Joins an Array Variant into a string.
Forwards to Array.join().
If the Variant is not already an Array, it will create a new Array from itself, and call join
on that Array.
Do no remove, this fixes the anchor on doc.flexsim.com
Description
Matches a reqular expression on a string Variant.
Forwards to string.match().
An exception is thrown if the Variant is not a string type.
Do no remove, this fixes the anchor on doc.flexsim.com
Parameters
targetLength |
The desired length of the padded string. |
padString |
The string used to pad the current string. |
Returns
string
|
The string padded to the specified length. |
Description
Pads the end of a string Variant with the padString to the given length.
Forwards to
string.
padEnd.
An exception is thrown if the Variant is not a string type.
Do no remove, this fixes the anchor on doc.flexsim.com
Parameters
targetLength |
The desired length of the padded string. |
padString |
The string used to pad the current string. |
Returns
string
|
The string padded to the specified length. |
Description
Pads the start of a string Variant with the padString to the given length.
Forwards to
string.
padStart.
An exception is thrown if the Variant is not a string type.
Do no remove, this fixes the anchor on doc.flexsim.com
Description
Pops an element from an Array Variant.
Forwards to Array.pop().
If the Variant is not an Array, it will convert itself into a null Variant, and return its
original value.
Do no remove, this fixes the anchor on doc.flexsim.com
Returns
Description
Pushes a value onto an Array Variant.
Forwards to Array.push().
If the Variant is a non-array value, it will convert itself into an Array, and forward to
Array.push(). If it is a null Variant,
it will become the passed value (not an Array).
Do no remove, this fixes the anchor on doc.flexsim.com
Description
Repeats a string Variant a number of times.
Forwards to string.repeat().
An exception is thrown if the Variant is not a string type.
Do no remove, this fixes the anchor on doc.flexsim.com
Parameters
findStr |
|
replaceWith |
|
findPattern |
|
Description
Replaces text in a string Variant
Forwards to string.replace().
An exception is thrown if the Variant is not a string type.
Do no remove, this fixes the anchor on doc.flexsim.com
Description
Reverses an Array Variant
Forwards to Array.reverse().
If the Variant is a non-array value, it will convert itself into an Array, and forward to
Array.push(). If it is a null Variant,
it will become the passed value (not an Array).
Do no remove, this fixes the anchor on doc.flexsim.com
Parameters
str |
The string or pattern to search for. |
Returns
int
|
If found, the 1-based byte index of the found string. Otherwise -1. |
Description
Searches a string Variant for an internal string or pattern.
Forwards to
string.
search.
An exception is thrown if the Variant is not a string type.
Do no remove, this fixes the anchor on doc.flexsim.com
Description
Shifts (pops) an element from the front of an Array Variant.
Forwards to Array.shift().
If the Variant is not an Array, it will convert itself into a null Variant, and return its
original value.
Do no remove, this fixes the anchor on doc.flexsim.com
Description
Returns a new Array made up of a subsection of an Array Variant.
Forwards to Array.slice().
If the Variant is not an Array, it will create an Array from itself and forward to
Array.slice().
Do no remove, this fixes the anchor on doc.flexsim.com
Returns
Description
Removes and/or inserts elements into an Array Variant.
Forwards to Array.splice().
If the Variant is not an Array, it will make itself into an Array and forward to
Array.splice().
Do no remove, this fixes the anchor on doc.flexsim.com
Description
Splits a string Variant into an Array.
Forwards to string.split().
An exception is thrown if the Variant is not a string type.
Do no remove, this fixes the anchor on doc.flexsim.com
Description
Queries if a string Variant starts with a defined string.
Forwards to string.startsWith().
An exception is thrown if the Variant is not a string type.
Do no remove, this fixes the anchor on doc.flexsim.com
Parameters
startIndex |
The 1-based byte index of the first character to extract.
If this index is in the middle of a multi-byte character, it will extract the full character.
If it is a negative number, the start position is string.length + beginIndex,
or in other words it is an offset from the end of the string. |
length |
The length of the target string, in bytes. |
Returns
Description
Extracts a section of a string Variant and returns a new string.
Forwards to
string.
substr.
An exception is thrown if the Variant is not a string type.
Do no remove, this fixes the anchor on doc.flexsim.com
Description
Creates a lower case string from a string Variant.
Forwards to string.toLowerCase().
An exception is thrown if the Variant is not a string type.
Do no remove, this fixes the anchor on doc.flexsim.com
Returns
double
|
The interpreted content of the string as a double. |
Description
Interprets a string Variant's content as a floating point number.
Forwards to
string.
toNum.
An exception is thrown if the Variant is not a string type.
Do no remove, this fixes the anchor on doc.flexsim.com
Description
Creates an upper case string from a string Variant.
Forwards to string.toUpperCase().
An exception is thrown if the Variant is not a string type.
Do no remove, this fixes the anchor on doc.flexsim.com
Description
Trims white space off of the ends of a string Variant.
Forwards to string.trim().
An exception is thrown if the Variant is not a string type.
Do no remove, this fixes the anchor on doc.flexsim.com
Returns
Description
Unshifts (pushes) a value onto the front of an Array Variant.
Forwards to Array.unshift().
If the Variant is a non-array value, it will convert itself into an Array, and forward to
Array.unshift(). If it is a null Variant,
it will become the passed value (not an Array).
Do no remove, this fixes the anchor on doc.flexsim.com
Parameters
value |
The value to subtract from the Variant |
Returns
double
|
The result of the subtraction. |
Description
Subtracts two numbers.
The following example uses
treenode.labelProperties to
access an object's label value.
double myVal = current.labelValue - 2;
Do no remove, this fixes the anchor on doc.flexsim.com
Description
Decrements a number Variant.
The following example uses
treenode.labelProperties to
decrement an object's label value.
current.labelValue--;
Do no remove, this fixes the anchor on doc.flexsim.com
Returns
int
|
True if the Variant is a null Variant, a NULL treenode, or the number 0.0. False otherwise. |
Description
Tests if a Variant is either a null Variant, a NULL treenode, or a 0.0 number.
The following example uses
treenode.labelProperties to
compare an object's label value.
if (!current.labelValue) {
...
}
Do no remove, this fixes the anchor on doc.flexsim.com
Parameters
value |
The value to compare against. |
Returns
int
|
True if the Variant is not equal to value, false otherwise. |
Description
Tests if a Variant is not equal to another value.
The following example uses
treenode.labelProperties to
compare an object's label value.
if (current.labelValue != 5) {
...
}
Do no remove, this fixes the anchor on doc.flexsim.com
Parameters
value |
The value used as the modulus. |
Returns
int
|
The modulus of the Variant and the other value. |
Description
Returns the modulus of two numbers.
The modulus operator returns the remainder of a division.
Do no remove, this fixes the anchor on doc.flexsim.com
Parameters
value |
The value to multiply by. |
Returns
double
|
The multiplication product of the two values. |
Description
Multiplies two numbers.
The following example uses
treenode.labelProperties to
access an object's label value.
double myVal = current.labelValue * 2;
Do no remove, this fixes the anchor on doc.flexsim.com
Parameters
value |
The value to divide the Variant by. |
Returns
double
|
The result of the division. |
Description
Divides two numbers.
The following example uses
treenode.labelProperties to
access an object's label value.
double myVal = current.labelValue / 2;
Do no remove, this fixes the anchor on doc.flexsim.com
Parameters
index |
The index of the array element. |
Returns
Variant
|
The array element at the specified index. |
Description
Accesses an element of an Array Variant.
Forwards to the Array [] operator.
The Variant implements special logic associated with arrays in that you can treat a Variant like an
Array even if it does not hold array data. If a Variant holds, for example,
a string, you can treat the Variant as if it were a 1-length Array with a
string as its first element.
Variant val = "Hello";
int len = val.length; // len = 1
return val[len]; // returns "Hello";
Do no remove, this fixes the anchor on doc.flexsim.com
Parameters
value |
The value to add to the Variant. |
Returns
Description
Adds two numbers.
The following example uses
treenode.labelProperties to
access an object's label value.
double myVal = current.labelValue + 2;
Do no remove, this fixes the anchor on doc.flexsim.com
Description
Increments a number Variant
The following example uses
treenode.labelProperties to
increment an object's label value.
current.labelValue++;
Do no remove, this fixes the anchor on doc.flexsim.com
Parameters
value |
The value to add to the Variant. |
Description
Increments a number Variant by a defined value, or appends a string to the current string Variant.
If both operands are strings, then the Variant will append value to itself. If both are numbers,
the Variant will increment its value by value.
The following example uses
treenode.labelProperties to
access an object's label value.
double myVal = current.labelValue - 2;
Do no remove, this fixes the anchor on doc.flexsim.com
Parameters
Returns
Description
Tests if a Variant is less than a number.
The following example uses
treenode.labelProperties to
compare an object's label value.
if (current.labelValue < 5) {
...
}
Do no remove, this fixes the anchor on doc.flexsim.com
Parameters
Returns
Description
Tests if a Variant is less than or equal to a number.
The following example uses
treenode.labelProperties to
compare an object's label value.
if (current.labelValue <= 5) {
...
}
Do no remove, this fixes the anchor on doc.flexsim.com
Parameters
value |
The value to decrement the Variant by. |
Description
Decrements a number Variant by a defined value.
The following example uses
treenode.labelProperties to
decrement an object's label value.
current.labelValue -= 2;
Do no remove, this fixes the anchor on doc.flexsim.com
Parameters
value |
The value to compare against. |
Returns
int
|
True if the Variant is equal to value, false otherwise. |
Description
Tests if two values are equal to each other.
The following example uses
treenode.labelProperties to
compare an object's label value.
if (current.labelValue == 5) {
...
}
Do no remove, this fixes the anchor on doc.flexsim.com
Parameters
value |
The value to compare against. |
Returns
int
|
True if the Variant is greater than value, false otherwise. |
Description
Tests if a Variant is greater than a number.
The following example uses
treenode.labelProperties to
compare an object's label value.
if (current.labelValue > 5) {
...
}
Do no remove, this fixes the anchor on doc.flexsim.com
Parameters
value |
The value to compare against. |
Returns
int
|
True if the Variant is greater than or equal to value, false otherwise. |
Description
Tests if a Variant is greater than or equal to a number.
The following example uses
treenode.labelProperties to
compare an object's label value.
if (current.labelValue >= 5) {
...
}