Creating and initializing Variants
All Variant variables should be initialized before they are used. Initialization sets the VT-Type of a variant to VT_EMPTY. f90VB offers a very convenient function (VariantCreate) that returns an initialized Variant variable, and also gives you the option of setting its type and value in a single operation. In its most basic syntax, you can use VariantCreate to initialize MyVar as follows:
MyVar = VariantCreate(iRet)
When VariantCreate is called this way, it returns a Variant that has been initialized to VT_EMPTY. As with other f90VB procedures, argument iRet can be used to check whether the operation was successful or not (the value returned in iRet is zero when the function succeeds).
In most cases, you will want to initialize a variant to a given type and value. To do this, you can use the extended forms of VariantCreate. The following example initializes MyVar to a Variant containing a long (four-byte) integer and the value 10:
MyVar = VariantCreate(VT_I4, 10, iRet)
VariantCreate can perform conversions of initial values on-the-fly. For example, the following expressions would have the same effect as the expression above:
MyVar = VariantCreate(VT_I4, ’10’, iRet)
MyVar = VariantCreate(VT_I4, ’10.0’, iRet)
MyVar = VariantCreate(VT_I4, 10.0, iRet)
In the examples below, we create a variant containing a BString:
MyVar = VariantCreate(VT_BSTR, 10, iRet)
MyVar = VariantCreate(VT_BSTR,’10’,iRet)
Although VariantCreate is possibly the most convenient way to initialize a Variant variable, you can also perform this operation by directly manipulating the fields of the variant structure. To initialize an empty variant you can use the following statement:
MyVar%varVal%vt = VT_EMPTY
To set the Variant to a long integer with value 10, you can assign the values of the vt field and lVal fields individually:
MyVar%varVal%vt = VT_I4
MyVar%varVal%lVal=10
The method you use is up to you. VariantCreate has the advantage of offering a single function that performs automatic conversion and that will catch assignment of incompatible types and warn you about it through the iRet argument. The complete syntax of function VariantCreate is described in detail in the f90VB reference manual.
Checking the type and obtaining the contents of a Variant