We help IT Professionals succeed at work.

What is the problem with this code -Under ATL- ?

MohdAsalah
MohdAsalah asked
on
HI all,
Can anybody tell me what is the problem with this code??
 _variant_t v1,v2;
 v1 = Function1("string1") ;
 v2 = Function2("string2");
 nEndYear = v1.intVal + v2.intVal;

When I debug this code I notice v1 equal 14.000 VT_R8 and v2 equal 32.000 VT_R8
but when add v1 to v2 the nEndYear become 0.

Comment
Watch Question

You have to add v1.dblVal+v2.dblVal

--EC--
Or you can use _variant_t double extractor:
Double v3 = (double)v1 + (double)v2;

--EC--

Author

Commented:
But how can I know if the variant value v1 and v2 have a double value not integral value????
1. You should know what you put in it, i.e. you know what Function1() and Function2() return.

2. You can check the VARTYPE value of the variants and switch/case what you get. e.g. :
VARTYPE vt = v1.vt;
switch vt
{
case VT_I4: //int
  //do something
  break;
case VT_R8: //double
  //do something else
  break;
//and so on......
}

3. When using _variant_t you can use extractors (like double(), long() etc.) To extracts raw data from an encapsulated VARIANT. If the VARIANT is not already the proper type, VariantChangeType() is used to attempt a conversion, and an error is generated upon failure.

--EC--
? When using _variant_t you can use extractors (like double
>(), long() etc.) To extracts raw data from

so, your the code you have would work if you wrote it like this:

nEndYear = static_cast<long>(v1) + static_cast<long>(v2);

Explore More ContentExplore courses, solutions, and other research materials related to this topic.