Special behavior of the Variant data type

<< Click to Display Table of Contents >>

Special behavior of the Variant data type

In SoftMaker Basic, a variable does not necessarily have to be declared before it is used for the first time (exception: if the Explicit option is set). SoftMaker Basic then automatically declares it on its first occurrence - as variant data type.

The Variant data type can be used to store either numbers or character strings or date/time values. Type conversion is performed automatically whenever needed.

You can also explicitly declare variables to be of the variant type, for example with Dim x As Variant or simply with Dim x.

An example for the use of variant variables:

Sub Main

 Dim x           ' Variant variable

 x = 10

 x = x + 8

 x = "F" & x

 Print x         ' Result: "F18"

End Sub

When numbers are stored in a variant variable, SoftMaker Basic automatically chooses the most compact data type possible. As a result, numbers will be represented as one of the following (in this order): Integer, Long, Single, Double.

The data type used by a variant variable can change at any time. You can use the VarType function to determine the current data type. You can use the IsNumeric function to check whether the variable currently contains a numeric value.

Variant variables can take two special values which are not available in other data types:

Empty is the value of a variant variable that has not yet been initialized. It can be queried with the IsEmpty function. In numeric operations, Empty is interpreted as 0, in string operations as an empty string.

The value Null serves to signal the fact that no (valid) value is available. It can be queried with the function IsNull. Each operation with a Null value results in Null.

Concatenating Variant variables

If you use the + operator on a text string and a number, the result will be a text string.

If you use the + operator on two numbers, the result will be a number. If you wish to receive a text string instead, use the & operator in place of +. This operator will always return a text string, independent of the data type.