none
How to use 'ChangeType'

    Question

  • I found this code sample and it used  'ChangeType'

        _variant_t vName; 
        CString strName; 
        vName = pRs-> GetCollect(_variant_t("Name"));
        vName.ChangeType(VT_BSTR);
        strName = vName.bstrVal;
    

    So I tried a 'ChangeType' experiment to understand it better...

    	_variant_t myVal1, myVal2;
    	myVal1=6; myVal2=8;
    	myVal1.ChangeType(int);
    	myVal2.ChangeType(int);
    	cout<<myVal1 + myVal2<<endl;
    	getchar();
    

    But it doesn't work. What is the correct syntax?

    Thanks for any help.

    Saturday, May 26, 2012 3:25 PM

Answers

All replies

  • Always check the documentation in MSDN.  Google found it as the 3rd search result:  http://msdn.microsoft.com/en-us/library/2kc812h8(VS.80).aspx.

    In the documentation, you'll see that the first argument is a number.  Use any VT_ constant here as long as it makes sense.  If you are confused about VT_'s and variants in general, then I would recommend studying the variants subject a bit more as it is kind of hard to squeeze it in a single forum post.  Good luck.


    Jose R. MCP

    Saturday, May 26, 2012 3:30 PM
  • Mel_3 wrote:

    So I tried a 'ChangeType' experiment to understand it better...

    [code]
    _variant_t myVal1, myVal2;
    myVal1=6; myVal2=8;
    myVal1.ChangeType(int);

    ChangeType accepts a constant from VARTYPE enumeration, not a type name.

    But it doesn't work. What is the correct syntax?

    That rather depends on what you are trying to achieve. You already have  a VARIANT holding an int - why do you want to change its type to the  same one it already stores?


    Igor Tandetnik

    Saturday, May 26, 2012 3:35 PM
  • webjose, I looked at the you suggested (and several others) link before I posted.

    Igor, Woops! I left off the next line which attempted to add the two values... here is the whole thing...

    _variant_t myVal1, myVal2;
    myVal1=6; myVal2=8;
    myVal1.ChangeType(int);
    myVal2.ChangeType(int);
    cout<<myVal1 + myVal2<<endl;
    getchar();

    I'm studying the webjose's link again but seeing my attempt properly done would be a help :)

    thanks guys.

    Saturday, May 26, 2012 3:42 PM
  • If you open Visual Studio, then open a C++ project and type VT_BSTR, you'll be able to get to its definition by hitting the F12 key or right-clicking, then selecting Go to definition.  That should open WTypes.h and show you this:

    enum VARENUM
        {	VT_EMPTY	= 0,
    	VT_NULL	= 1,
    	VT_I2	= 2,
    	VT_I4	= 3,
    	VT_R4	= 4,
    	VT_R8	= 5,
    	VT_CY	= 6,
    	VT_DATE	= 7,
    	VT_BSTR	= 8,
    	VT_DISPATCH	= 9,
    	VT_ERROR	= 10,
    	VT_BOOL	= 11,
    	VT_VARIANT	= 12,
    	VT_UNKNOWN	= 13,
    	VT_DECIMAL	= 14,
    	VT_I1	= 16,
    	VT_UI1	= 17,
    	VT_UI2	= 18,
    	VT_UI4	= 19,
    	VT_I8	= 20,
    	VT_UI8	= 21,
    	VT_INT	= 22,
    	VT_UINT	= 23,
    	VT_VOID	= 24,
    	VT_HRESULT	= 25,
    	VT_PTR	= 26,
    	VT_SAFEARRAY	= 27,
    	VT_CARRAY	= 28,
    	VT_USERDEFINED	= 29,
    	VT_LPSTR	= 30,
    	VT_LPWSTR	= 31,
    	VT_RECORD	= 36,
    	VT_INT_PTR	= 37,
    	VT_UINT_PTR	= 38,
    	VT_FILETIME	= 64,
    	VT_BLOB	= 65,
    	VT_STREAM	= 66,
    	VT_STORAGE	= 67,
    	VT_STREAMED_OBJECT	= 68,
    	VT_STORED_OBJECT	= 69,
    	VT_BLOB_OBJECT	= 70,
    	VT_CF	= 71,
    	VT_CLSID	= 72,
    	VT_VERSIONED_STREAM	= 73,
    	VT_BSTR_BLOB	= 0xfff,
    	VT_VECTOR	= 0x1000,
    	VT_ARRAY	= 0x2000,
    	VT_BYREF	= 0x4000,
    	VT_RESERVED	= 0x8000,
    	VT_ILLEGAL	= 0xffff,
    	VT_ILLEGALMASKED	= 0xfff,
    	VT_TYPEMASK	= 0xfff
        } ;
    typedef ULONG PROPID;
    

    Those are the "types" you can use in ChangeType().  Not all, actually, only a subset.  To undesrstand which ones you should study COM and VARIANT more in depth.

    Your failed sample consists of a variant storing an integer of type VT_I4, which is a 4-byte signed integer, and is the exact same thing as an int as Igor pointed out.  It would make sense to change it to a BSTR (VT_BSTR) or maybe even a date (VT_DATE), but trying to convert to int is not because you already have an int.


    Jose R. MCP

    Saturday, May 26, 2012 4:02 PM
  • Mel_3 wrote:

    Igor, Woops! I left off the next line which attempted to add the two  values... here is the whole thing...

    _variant_t myVal1, myVal2;
    myVal1=6; myVal2=8;
    cout<<myVal1 + myVal2<<endl;

    cout << int(myVal1) + int(myVal2) << endl;


    Igor Tandetnik

    Saturday, May 26, 2012 6:12 PM
  • webJose... your right, I am confused about the definitions & use of things like  BSTR and VT_BSTR

    - And I thought _variant_t was simply a predefined type to hold a variant

    - And to me a variant type can hold characters, numbers, etc...

    - And, how do you know what size each of the 'types' you listed are?
      (you said vt_14... which is VT_DEMICAL (right?)  was 4 bytes long)

    - I've Googl'd a lot but end up getting bogged down... and have 3 C++ Books sitting here
      and none of them were any help.
      None mention "BSTR"  or VT_anything or underbar anything like _variant_t in the Index.

    - I wish I had time to just stop and dig in more right now but the schedule won't allow it... have to pick it up as I go for now...

    Igor, thanks for the example! Very nice. I had just come up with a a work-around which is in essence the same solution...

    _variant_t myVal1, myVal2;
    myVal1=6; myVal2=8;
    //myVal1.ChangeType(int); //bad syntax
    //myVal2.ChangeType(int); //bad syntax
    //cout<<myVal1 + myVal2<<endl; //won't add
    int x=myVal1; //try this
    int y=myVal2; //and this
    cout <<x+y<<endl; //worked!
    getchar();


    So in a pinch this seems to be a work-around... but I still have a lot to learn !

    Thanks for the help guys.


    Saturday, May 26, 2012 7:53 PM
  • Mel_3 wrote:

    - And, how do you know what size each of the 'types' you listed are?
    (you said vt_14... which is VT_DEMICAL (right?) was 4 bytes long)

    A variant of type VT_I4 holds a 32-bit (4-byte) signed integer. A  variant of type VT_DECIMAL holds a DECIMAL structure, which represents a  fractional number.

    Igor, thanks for the example! Very nice. I had just come up with a a  work-around which is in essence the same
    solution..._variant_t myVal1, myVal2; myVal1=6; myVal2=8;
    //myVal1.ChangeType(int);//bad syntax
    //myVal2.ChangeType(int);//bad syntax
    //cout<<myVal1 + myVal2<<endl; //won't add
    int x=myVal1; //try this
    int y=myVal2; //and this
    cout <<x+y<<endl;//worked!

    Why are you packing your integers into variants, only to immediately get  them right out again? What's the point of the exercise?


    Igor Tandetnik

    Saturday, May 26, 2012 8:12 PM