[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Anyone understand the "Variant" datatype???

Posted on 2004-11-26
4
Medium Priority
?
307 Views
Last Modified: 2012-06-22
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.

Thanks!

http://www.experts-exchange.com/Programming/Programming_Languages/Perl/Q_21220762.html
0
Comment
Question by:sapbucket
4 Comments
 
LVL 8

Assisted Solution

by:rk_radhakrishna
rk_radhakrishna earned 1000 total points
ID: 12685451
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:
   DIM V1 AS VARIANT, V2 AS VARIANT

   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,
   DIM V1 AS VARIANT, V2 AS VARIANT

   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:
http://www.basicguru.com/rapidq/docs/datatypes.html
http://www.thesa.com/th/th-78-73-175-th-21-5-134.htm
0
 
LVL 4

Accepted Solution

by:
ramu_src2k earned 1000 total points
ID: 12689732
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:

VARIANT v;
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:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/automat/htm/chap6_7zdz.asp

Cheers...
0
 
LVL 7

Expert Comment

by:aib_42
ID: 12694117
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.
0
 
LVL 3

Author Comment

by:sapbucket
ID: 12848297
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.

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Suggested Courses

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question