Variant variables are now supported by PowerBASIC, but their use is limited to that of a parameter assignment for conversion of data, for compatibility with other languages and applications, especially COM Objects.
Although notoriously lacking in efficiency, Variants are commonly used
as COM Object parameters due to their flexibility. You can think
of a Variant as a kind of container, which can hold a variable of most
any data type,
This flexibility comes at a great price in performance, so PowerBASIC limits their use to data storage and parameters only. You may assign a numeric value, a string value, or even an entire array to a Variant with the LET statement, or its implied equivalent. In the same way, you may assign one Variant value to another Variant variable, or even assign an array contained in a Variant to a compatible PowerBASIC array, or the reverse.
You may extract a simple scalar value from a Variant with the VARIANT# function for numeric values (regardless of the internal numeric data type), or with the VARIANT$ function for string values. You may determine the type of data a Variant variable contains with the VARIANTVT function. The following table summarizes the predefined (built-in) equates that can be used to examine a Variant:
Result |
Equate |
Content Type |
0 |
%VT_EMPTY |
0 or Empty string |
1 |
%VT_NULL |
Null string |
2 |
%VT_I2 |
|
3 |
%VT_I4 |
|
4 |
%VT_R4 |
|
5 |
%VT_R8 |
|
6 |
%VT_CY |
|
7 |
%VT_DATE |
Date |
8 |
%VT_BSTR |
|
9 |
%VT_DISPATCH |
|
10 |
%VT_ERROR |
Error Code |
11 |
%VT_BOOL |
|
12 |
%VT_VARIANT |
|
13 |
%VT_UNKNOWN |
|
16 |
%VT_I1 |
|
17 |
%VT_UI1 |
Byte |
18 |
%VT_UI2 |
|
19 |
%VT_UI4 |
|
20 |
%VT_I8 |
Quad (signed) |
21 |
%VT_UI8 |
Quad (unsigned) |
22 |
%VT_INT |
Integer |
23 |
%VT_UNIT |
Word |
24 |
%VT_VOID |
|
25 |
%VT_HRESULT |
COM result code |
26 |
%VT_PTR |
|
27 |
%VT_SAFEARRAY |
VB Array |
28 |
%VT_CARRAY |
|
29 |
%VT_USERDEFINED |
|
30 |
%VT_LPSTR |
|
31 |
%VT_LPWSTR |
|
64 |
%VT_FILETIME |
|
65 |
%VT_BLOB |
|
66 |
%VT_STREAM |
|
67 |
%VT_STORAGE |
|
68 |
%VT_STREAMED_OBJECT |
|
69 |
%VT_STORED_OBJECT |
|
70 |
%VT_BLOB_OBJECT |
|
71 |
%VT_CF |
|
72 |
%VT_CLSID |
Class ID |
&H1000 |
%VT_VECTOR |
|
&H2000 |
%VT_ARRAY |
Array |
&H4000 |
%VT_BYREF |
|
Variants may not be used in an expression, be directly output (PRINT#, etc), or used as a member of a structure such as a User-Defined Type (UDT) or UNION, etc. Instead, you must first extract the value with one of the above conversion functions, and use that acquired value for calculations.
Internally, a Variant is always 16-bytes in size, and may be passed as either a BYVAL or a BYREF parameter, at the programmer's discretion. However, when a BYREF Variant is required as a parameter, only an explicit Variant variable may be passed by the calling code - a BYCOPY expression is not allowed.
All dynamic strings contained in a Variant must be Wide/Unicode, and PowerBASIC handles these conversions automatically through the LET statement and its implied equivalent.
There may be some cases where you wish to manipulate the internal structure of a Variant directly. Though possible, you must exercise caution or a serious memory leak could occur. Since a Variant could be the owner of a string, array, etc., you must always reset a Variant ([LET] VrntName = EMPTY) prior to manipulation with POKE, or pointers, etc.
When you use the standard PowerBASIC assignment syntax, for example: [LET] VrntName = 21, all this "housekeeping" is completely automatic and handled by PowerBASIC for you.
Every Variant variable must be explicitly declared with an appropriate statement such as:
DIM xyz AS VARIANT or LOCAL xyz AS VARIANT
See Also