Variant

As is the case with Visual Basic, when a variable is introduced in Cypress Enable, it is not necessary to declare it first (see option explicit for an exception to this rule). When a variable is used but not declared then it is implicitly declared as a variant data type. Variants can also be declared explicitly using "As Variant" as in Dim x As Variant. The variant data type is capable of storing numbers, strings, dates, and times. When using a variant you do not have to explicitly convert a variable from one data type to another. This data type conversion is handled automatically.

For example:

Sub Main

Dim x                    'variant variable

x = 10

x = x + 8

x = "F" & x

print x                    'prints F18

End Sub

A variant variable can readily change its type and its internal representation can be determined by using the function VarType. VarType returns a value that corresponds to the explicit data types. See "VarType" in the "Language Reference A - Z" chapter for return values.

When storing numbers in variant variables the data type used is always the most compact type possible.

For example, if you first assign a small number to the variant, it is stored as an integer. If you then assign your variant to a number with a fractional component, it is stored as a double.

For doing numeric operations on a variant variable it is sometimes necessary to determine if the value stored is a valid numeric, thus avoiding an error. This can be done with the IsNumeric function described in the "IsNumeric" topic in the "Language Reference A - Z" chapter.