Variant Data Type

Source: Internet
Author: User
The variant data type is defined in the file oaidl. IDL as follows:

Struct tagvariant {
Union {
Struct _ tagvariant {
Vartype VT;
Word wreserved1;
Word wreserved2;
Word wreserved3;
Union {
Ulonglong ullval;/* vt_ui8 */
Longlong llval;/* vt_i8 */
Long lval;/* vt_i4 */
Byte bval;/* vt_ui1 */
Short ival;/* vt_i2 */
Float fltval;/* vt_r4 */
Double dblval;/* vt_r8 */
Variant_bool boolval;/* vt_bool */
_ Variant_bool bool;/* (obsolete )*/
Scode;/* vt_error */
Cy cyval;/* vt_cy */
Date;/* vt_date */
BSTR bstrval;/* vt_bstr */
Iunknown * punkval;/* vt_unknown */
Idispatch * pdispval;/* vt_dispatch */
Safearray * parray;/* vt_array */
Byte * pbval;/* vt_byref | vt_ui1 */
Short * pival;/* vt_byref | vt_i2 */
Long * plval;/* vt_byref | vt_i4 */
Longlong * pllval;/* vt_byref | vt_i8 */
Float * pfltval;/* vt_byref | vt_r4 */
Double * pdblval;/* vt_byref | vt_r8 */
Variant_bool * pboolval;/* vt_byref | vt_bool */
_ Variant_bool * pbool;/* (obsolete )*/
Scode * pscode;/* vt_byref | vt_error */
Cy * pcyval;/* vt_byref | vt_cy */
Date * pdate;/* vt_byref | vt_date */
BSTR * pbstrval;/* vt_byref | vt_bstr */
Iunknown ** ppunkval;/* vt_byref | vt_unknown */
Idispatch ** ppdispval;/* vt_byref | vt_dispatch */
Safearray ** pparray;/* vt_byref | vt_array */
Variant * pvarval;/* vt_byref | vt_variant */
Pvoid byref;/* generic byref */
Char cval;/* vt_i1 */
Ushort uival;/* vt_ui2 */
Ulong ulval;/* vt_ui4 */
Int intval;/* vt_int */
Uint uintval;/* vt_uint */
Decimal * pdecval;/* vt_byref | vt_decimal */
Char * pcval;/* vt_byref | vt_i1 */
Ushort * puival;/* vt_byref | vt_ui2 */
Ulong * pulval;/* vt_byref | vt_ui4 */
Ulonglong * pullval;/* vt_byref | vt_ui8 */
Int * pintval;/* vt_byref | vt_int */
Uint * puintval;/* vt_byref | vt_uint */
Struct _ tagbrecord {
Pvoid pvrecord;
Irecordinfo * precinfo;
} _ Variant_name_4;/* vt_record */
} _ Variant_name_3;
} _ Variant_name_2;

Decimal decval;
} _ Variant_name_1;
};
The variant data structure contains two fields (if reserved fields are not considered ). The VT field describes the data type of the Second Field. To enable multiple types to appear in the second domain, we define a union structure. Therefore, the name of the second domain changes with the input value in the VT domain. Constants used to specify the value of the VT field are provided in the comments of each row in the Union definition.
The use of the variant and variantarg data structures must be completed in two steps. For example, let's consider the following code:
Long lvalues = 999;
Variant vparam;
Vparam. Vt = vt_i4;
Vparam. lval = lvalue;
Specify the data type in the first row. The constant vt_i4 indicates that a long data type will appear in the Second Field. According to the definition of type variant, we can know that when a long data is stored in the variant type, the second domain name is lval.
Passing parameters using variant means that non-strong languages (such as VBScript) can call methods implemented using strong languages (C ++. The implementation of the invoke () method can check whether the value encapsulated by the Variant Parameter conforms to its correct data type. If yes, this type is extracted and passed to the calling method. Otherwise, the invoke () method can use the variantchangetype () API function to convert the value to the correct type.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.