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!
Re: How to put Date into VARIANT data type?
Quote:
I tried doing this, but I get an error when accessing the function:
And what error do you get? :)
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.
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!
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?
Re: How to put Date into VARIANT data type?
[ off-topic ]
Quote:
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.