Variant Data Types

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, numeric, string, or even an entire array. This simplifies the process of calling procedures in a COM Object Server, as there is little need to worry about the myriad of possible data types for each parameter.

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$ and VARIANT$$ functions 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 | An Empty Variant
1 | %VT_NULL | Null value
2 | %VT_I2 | Integer
3 | %VT_I4 | Long-Integer
4 | %VT_R4 | Single
5 | %VT_R8 | Double
6 | %VT_CY | Currency
7 | %VT_DATE | Date
8 | %VT_BSTR | Dynamic String
9 | %VT_DISPATCH | IDispatch
10 | %VT_ERROR | Error Code
11 | %VT_BOOL | Boolean
12 | %VT_VARIANT | Variant
13 | %VT_UNKNOWN | IUnknown
14 | %VT_DECIMAL | Decimal
16 | %VT_I1 | Byte (signed)
17 | %VT_UI1 | Byte (unsigned)
18 | %VT_UI2 | Word
19 | %VT_UI4 | DWORD
20 | %VT_I8 | Quad (signed)
21 | %VT_UI8 | Quad (unsigned)
22 | %VT_INT | Long-Integer
23 | %VT_UINT | DWord
24 | %VT_VOID | A C-style void type
25 | %VT_HRESULT | COM result code
26 | %VT_PTR | Pointer
27 | %VT_SAFEARRAY | VB Array
28 | %VT_CARRAY | A C-style array
29 | %VT_USERDEFINED | User Defined Type
30 | %VT_LPSTR | ANSI string
31 | %VT_LPWSTR | Unicode string
64 | %VT_FILETIME | A FILETIME value
65 | %VT_BLOB | An arbitrary block of memory
66 | %VT_STREAM | A stream of bytes
67 | %VT_STORAGE | Name of the storage
68 | %VT_STREAMED_OBJECT | A stream that contains an object
69 | %VT_STORED_OBJECT | A storage object
70 | %VT_BLOB_OBJECT | A block of memory that represents an object
71 | %VT_CF | Clipboard format
72 | %VT_CLSID | Class ID
&H1000 | %VT_VECTOR | An array with a leading count
&H2000 | %VT_ARRAY | Array
&H4000 | %VT_BYREF | A reference value

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

Wha t is an object, anyway?

Just what is COM?

What is a COM component?