• C

Anyone understand the "Variant" datatype???

This is a cross-language question that I posted in the PERL channel. However, it has just as much to do with C, so I am allocating another 500 to see if anyone here can answer it from the C end.


Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Variant data type
A Variant is a special kind of data type, since a Variant can mutate. Don't be afraid, it won't mutate to a virus or anything like that. It simply changes types on the fly, as needed. Even a simple operation like addition changes when you add 2 variants together. Take this for example:

   V1 = 101
   V2 = "99.5"

   PRINT V1+V2  '-- Output is 200.5

As you'll notice V2 is originally assigned a string, but V2 has changed its type to a floating point number when the addition takes place. Note that V2 is still a string, it just mutated itself for that one operation. Similarly,

   V1 = "101"
   V2 = 99.5

   PRINT V1+V2  '-- Output is 10199.5

In this case, V2 was originally a floating point number, but changed its type during the addition operation to a string. Notice how the order of operation defines the mutation. In the previous example, V1 was a number, so V1+V2 results in a numeric expression, while the above example produces a string.

You can find some Information here:
Variant data type is a special data type that can contain numeric, string, or date data as well as user-defined types and the special values Empty and Null. The VARIANT type is an all purpose data type. The Variant data type has a numeric storage size of 16 bytes and can contain data up to the range of a Decimal, or a character storage size of 22 bytes (plus string length), and can store any character text.

Here is a simplified version of the VARIANT definition.

struct tagVARIANT {
    VARTYPE vt; // unsigned short integer type code
    union {
    //  C++ Type      Union Name   Type Tag                Basic Type
    //  --------      ----------   --------                ----------
        long          lVal;        // VT_I4                ByVal Long
        unsigned char bVal;        // VT_UI1               ByVal Byte
        short         iVal;        // VT_I2                ByVal Integer
        double        dblVal;      // VT_R8                ByVal Double
        VARIANT_BOOL  boolVal;     // VT_BOOL              ByVal Boolean
        SCODE         scode;       // VT_ERROR
        DATE          date;        // VT_DATE              ByVal Date
        BSTR          bstrVal;     // VT_BSTR              ByVal String
        IUnknown      *punkVal;    // VT_UNKNOWN
        SAFEARRAY     *parray;     // VT_ARRAY|*           ByVal array
        // A bunch of other types that don't matter here...
        VARIANT       *pvarVal;    // VT_BYREF|VT_VARIANT  ByRef Variant
        void          * byref;     // Generic ByRef        

Here is an example of creating a variant of type double:

v.vt= VT_R8;
v.dblVal = 999.999;

This example shows a variant containing all the actual data inside it's 16 byte structure. With the more complex string & safearry types, the data is stored separately and the VARIANT structure just contains a pointer to it.

You can find more details at this link:



Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Note that there is no data conversion of the data type in C. It is the programmer's responsibility to read the type he placed in the variable, else Bad Things start to happen.
sapbucketAuthor Commented:
Thanks for the comments!

For a COMPLETE programmers model of the Variant type read "Programming Microsoft Visual Basic 6.0" by Francesco Balena. Very good stuff. The comments here helped me to find his book.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.