Variants

Variant is a special data type: variables of type Variant can hold values of any of the following data types that LotusScript® recognizes, except for user-defined data types:

  • A value of any of the scalar data types that LotusScript® supports -- Boolean, Byte, Integer, Long, Single, Double, Currency, String
  • A date/time value
  • An array or list
  • An object reference, that is, a pointer to an OLE Automation object or to an instance of a product-defined or user-defined class, or an object reference to a Java Object.
  • The NULL value
  • The EMPTY value

You declare a Variant variable the same way you declare a scalar variable -- explicitly or implicitly. If no Deftype statements are applicable, a variable that you declare without using an As dataType clause or a data type suffix is of type Variant. Here, Variant variables appear with the suffix V to distinguish them from object reference variables or variables of some user-defined data type. For example:

Dim myVariant1V As Variant
Dim myVariant2V
Public myVariant3V As Variant
myVariant4V = 123.45

When you declare a Variant variable explicitly, LotusScript® initializes it to the special value EMPTY. (Use the function IsEmpty to test a Variant variable for this value.) Declaring a Variant variable is less efficient than assigning it another data type, but is convenient. When you assign a Variant variable a value, LotusScript® determines the data type of that value in either of two ways, depending on the available information:

  • If the data type of the value is known, then the value retains its original data type.
  • If the value is a literal, it is assigned a default data type appropriate to that value.

You can determine the data type of a value assigned to a Variant variable with the DataType or TypeName function, as in the following example:

Dim numVarV As Variant
Dim anAmount As Currency
anAmount@ = 20.05
numVarV = anAmount@
Print TypeName(numVarV)
' Output: CURRENCY
numVar = 20.05
Print TypeName(numVar)
' Output: DOUBLE

Under certain circumstances, the data type of a value assigned to a Variant variable can change to accommodate the requirements of a particular operation on it. For instance, in the following example the user enters a sequence of numeric characters, which are then treated as a String value for some operations and as a numeric value for others:

' Declare a Boolean variable and assign it an initial
' value of FALSE (0). The application subsequently tests
' this variable, taking appropriate action depending on the
' variable's value--True (-1) or False (0).
quitFlag = FALSE
Dim ansV As Variant
' Have the user enter some numeric characters.
ansV = InputBox("Enter a number.")
' See how many characters the user entered
' and assign that number to the Integer variable
' UB%. This involves treating the value of ansV
' as a String.
UB% = Len(ansV)
' Test the value of ansV to see if it can be
' interpreted as being of one of the numeric
' data types. If so, declare a dynamic array of Variants,
' then allocate space for as many elements as
' there are characters in ansV, and then assign
' the successive digits in ansV to the elements in
' the array. 
If IsNumeric(ansV) = True then
  Dim digitArrayV() As Variant
  ReDim digitArrayV(1 To UB%)As Variant
  For x% = 1 to UB%
     digitArrayV(x%) = Mid(ansV, x%, 1)
  Next
Else
   Print "You entered some nonnumeric characters."
   quitFlag = TRUE	
End If
' If ansV was able to be interpreted as a numeric,
' print its digits and their sum; then print
' the result of adding that sum to the original
' number that the user entered.
If quitFlag = False Then
   Dim theSum As Integer
   ' theSum% is initialized to 0.
   For x% = 1 to UB%
      theSum% = theSum% + digitArrayV(x%)
      Print digitArrayV(x%) ;
   Next
   Print ""
   Print "Their sum is: " & theSum%
   Print "Their sum added to the original number is: " _
     & ansV + theSum%
End If
' Output, supposing the user enters 12345:
' 12345
' Their sum is: 15
' Their sum added to the original number is: 12360