8.25.3 Variant Variable
You can use the Variant component class #PRIM_VAR to create a variant component variable. A variant component variable can contain any type of data (strings, integers, decimals, booleans, components).
A variant component has properties for testing the kind of value contained by the component. You can retrieve its value in converted forms like numbers, strings or booleans.
Variants make it possible for components to give access to values whose type cannot be statically determined. For example, the value of a cell in a grid cannot be statically defined - it depends on the type of the cell's column. By returning a variant, the grid control can provide access to the cell's value without causing compiler errors about the declared type of the value. You then need to write the program in a way that understands what type(s) are in the grid.
Variants are also used in dynamic programming. There are situations (especially with ActiveX) where the type information of a component cannot be determined at compile time. It must be processed at runtime. When using variants, the Visual LANSA compiler does not attempt to resolve the method or property because it has no idea what type of component is stored in the variant. It must wait until runtime to resolve all this information. This behavior enables the an ActiveX control to supply a component of unknown type and only when the component is called will the type information be requested.
This statement defines a variant variable called #MYVARIANT:
Define_Com Class(#PRIM_VAR) Name(#MYVARIANT)
You can assign a value to the variant variable either using its Value property (in which case the type of the value is of unspecific type):
Set Com(#myvariant) Value(#XYZ)
Or by explicitly assigning its value type using the String, Integer, Boolean, Decimal or Component properties of the variable:
Set Com(#myvariant) Integer(#XYZ)
Explicitly assigning the type of value is necessary only when you use the variable in RDML commands, in RDMLX the type can be unspecific.
When you read the value of the variable it is automatically converted to the type of the field that receives the value:
Set Com(#Out_INTEGER) Value(#myvariant)
Or you can also explicitly specify the type of value:
Set Com(#Out_INTEGER) Value(#myvariant.Integer)
To ensure that the value in the variant is one that you can support you use the ValueType property of the variable.
IF Cond('#myvariant.ValueType = VarInteger')
Set Com(#Out_INTEGER) Value(#myvariant)
ELSE
Set Com(#Out_INTEGER) Value(0)
ENDIF
If this check is not performed and the value cannot be converted at runtime, you will get a runtime error.
The ValueType property is an enumeration whose value can be one of the following symbols:
- varNull
- varEmpty
- varInteger
- varDouble
- varString
- varDecimal
- varBoolean
- varComponent