Re: Comparing a variant string with char*



Thanks for replying.
I understand what you are saying, but the above code compiles and runs
OK on VS2002. So I was wondering if there is a simpler way to make it
compile and run on VS2005.


Joseph M. Newcomer wrote:
Note that 'char' is a rather quaint and obsolete data type, to be used only in rare and
exotic conditions.

A VARIANT type is not a string, and is not comparable to a string; furthermore, in the
extremely unlikely situation where you would have a pointer to an 8-bit character string
that contains "Y", it will not compare as equal to the string you coded, "Y", because ==
compares pointers, not values.

There is no VARIANT type that can represent a char string, for example, although there is
a type, VT_BYREF | VT_UI1, that, in the very unlikely situation where the string returned
was really a 8-bit character string, might be used.

VARIANT is a union, which has many members. You have to first ascertain that the type of
variant you have indeed is a pointer to 8-bit characters (most VARIANT types will be
pointers to Unicode (BSTR), because most VARIANT types are used to communicate to systems
such as VB that are implicitly Unicode), and if it is, you have to use (because you are
assuming 8-bit characters), strcmp. You have to allow for the fact that the VARIANT might
be Unicode characters. strcmp/wcscmp/tcscmp compare the contents of strings.

So you might write something like
VARIANT * v = _getVariant();
if(v != NULL && v->vt == VT_BSTR)
{ /* it is a string */
if(wcscmp(v->bstrVal, L"Y") == 0)
{ /* "Y" */
... code here

This should all be obvious if you actually had read the documentation about the VARIANT
type. Note that you can't write the code you did because (a) it might return NULL (b) it
might return a VARIANT which is not VT_BSTR. You have to assume that both of these are
possible.
joe
On 18 Dec 2006 21:39:12 -0800, "Aviraj" <aviraj.singh@xxxxxxxxx> wrote:

I am trying to compare a variant string with a char* constant. The code
compiles on VS2002, but VS2005 gives the following compilation error,

Error 1 error C2678: binary '==' : no operator found which takes a
left-hand operand of type '_variant_t' (or there is no acceptable
conversion) c:\documents and settings\projects\test\test\test.cpp 25

The code is as follows,

static _variant_t _getVARIANTTfromRs();

int _tmain(int argc, _TCHAR* argv[])
{
if(_getVARIANTT() == "Y")
{
printf("Inside if\n");
}
else
{
printf("Inside else\n");
}

return 0;
}

_variant_t _getVARIANTT()
{
_variant_t value;
value.bstrVal = _bstr_t("");
return value;
}

How do I get this code to compile on VS2005 and make sure that the
comparison succeeds in case _getVARIANT returns "Y"

Thanks.
Joseph M. Newcomer [MVP]
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm

.



Relevant Pages

  • Re: Using the $ after functions
    ... AnyString = "Hello World" ' Define string. ... I would not need to use the variant that defined how many characters from ... for query expressions - I suggest to use variant type ...
    (microsoft.public.access.modulesdaovba)
  • Re: The Variant is back!!
    ... Its interesting all the comments of the "evils" of the Variant. ... not here to dispute! ... What's interesting is that SQL Server 2000 added a Variant type as a ... // add a string, becomes "4.3wow" ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: optional Safearray of UDTs from VB
    ... VB will fill the VARIANT ... > I am passing a safearray of UDT's from VB to my ATL / COM server. ... > SAFEARRAY*SequenceArr); // this works!! ... > This compiles, but and seems ok in the VB object viewer, but on VB compile ...
    (microsoft.public.vc.atl)
  • Re: Increment a Variable?
    ... >> No, it does not have to be a Variant type, but you can't change data ... >> types with a redim unless the array is contained in a Variant. ... > Dirk Goldgar, MS Access MVP ...
    (microsoft.public.access.formscoding)
  • Re: Invalid variant type conversion
    ... >> If there was no variant type, one would probably create one for ... who definately serve a purpose when you want to ... One may discuss the possible problem with coding with variants vs. the ... simple variant assignment may include 100's of instructions. ...
    (comp.lang.pascal.delphi.misc)