Convert int to _bstr_t
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Convert int to _bstr_t

  1. #1
    Join Date
    Feb 2001
    Posts
    51

    Convert int to _bstr_t

    How can I convert an int to a _bstr_t?


  2. #2
    Join Date
    Apr 2000
    Location
    San Francisco, California, USA
    Posts
    4,467

    Re: Convert int to _bstr_t

    int nNumber = 123;

    WCHAR szNumber[16];
    _bstr_t bstr = _itow(nNumber, szNumber, 10);



    Russian Software Development Network -- http://www.rsdn.ru

  3. #3
    Join Date
    Feb 2001
    Posts
    51

    Re: Convert int to _bstr_t

    Is there a disadvantage to type casting the int into a long?

    int nNumber = 123;
    _bstr_t bstr = (long)nNumber;



  4. #4
    Join Date
    Apr 2000
    Location
    San Francisco, California, USA
    Posts
    4,467

    Re: Convert int to _bstr_t

    But bstr_t has no constructor that accepts long. This code simply will
    not compile.

    Russian Software Development Network -- http://www.rsdn.ru

  5. #5
    Join Date
    Feb 2001
    Posts
    51

    Re: Convert int to _bstr_t

    Initially I found some code that converted a long to a _variant_t and then from a _variant_t to _bstr_t:
    int nNumber = 1234;
    _bstr_t mylong = (_variant_t)(long)nNumber;
    However, when I removed (_variant_t), the code still compiled. I realize that MSDN's description of _bstr_t does NOT include long, and MSDN's description of _variant_t does include long, but the code compiles and executes. Is it better to code it the other way?


  6. #6
    Join Date
    Apr 2000
    Location
    San Francisco, California, USA
    Posts
    4,467

    Re: Convert int to _bstr_t

    I've just tested it and found that

    _bstr_t mylong = (long)nNumber;

    works exactly as

    _bstr_t mylong = (_variant_t)(long)nNumber;

    It is a bit strange for me. You can use either method.

    Russian Software Development Network -- http://www.rsdn.ru

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center