CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Mar 2010
    Posts
    42

    How to put Date into VARIANT data type?

    Hi all,



    I want to insert a Date type into a VARIANT that will be used by a certain function that needs a Date, but requires a VARIANT* data type.



    The function looks something like this:



    _variant_t function(_bstr_t FriendlyName, _bstr_t SubjectName, _bstr_t SMSID, VARIANT *StartTime, VARIANT *EndTime)



    I can't do anything about the data type because it is a pre-defined function.



    I tried doing this, but I get an error when accessing the function:



    VARIANT varDate;

    VariantInit(&varDate);



    COleDateTime mytime(1996,1,1,0,0,0);

    varDate = _variant_t(mytime);



    Also when accessing the function, I do it like this:



    ->function(bstrName, bstrName2, bstrID, &varDate, &varDate);



    I hope somebody can help me.



    Thank you!

  2. #2
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: How to put Date into VARIANT data type?

    I tried doing this, but I get an error when accessing the function:
    And what error do you get?
    Best regards,
    Igor

  3. #3
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: How to put Date into VARIANT data type?

    Code:
    varDate.vt = VT_DATE;
    varDate.date = mytime;
    Yep, this is exactly what _variant_t(mytime) is supposed to do.
    Best regards,
    Igor

  4. #4
    Join Date
    Mar 2010
    Posts
    42

    Re: How to put Date into VARIANT data type?

    Hi Igor and 0xc0000005,

    Thanks for replying and the solutions! What worked was:

    varDate = _variant_t(mytime, VT_DATE);

    Another solution (but with more lines of code):

    VARIANT var;
    V_VT(&var) = VT_DATE;
    V_DATE(&var) = myDateValue;

    Thanks again very much!

  5. #5
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: How to put Date into VARIANT data type?

    Beside the solutions already posted here, the most handy way to deal with VARIANT structures in MFC is to use COleVariant.
    Just:
    Code:
       COleDateTime mytime(1996, 1, 1, 0, 0, 0);
       function(COleVariant(mytime));
       // COleVariant has a constructor which takes const COleDateTime& and an operator LPVARIANT().
    A little bit easier, isn't it?
    Last edited by ovidiucucu; September 12th, 2010 at 07:19 AM.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  6. #6
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: How to put Date into VARIANT data type?

    [ off-topic ]
    The Cardinal Rules of Optimization:
    1. Do not optimize
    2. For experts only: do not optimize yet
    Except it's otherwise specified, a clearer/shorter/lesser susceptible for bugs/easier to read and maintain code, has to be preffered to "more efficient" one.
    Last edited by ovidiucucu; September 12th, 2010 at 08:01 AM.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

Tags for this Thread

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured