Re: Comparing a variant string with char*



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: Code to delete/unlink Linked tables
    ... Public intLinkODBCTables As Variant, intLinkDB2Tables As Variant ... Public strLinkBackendDB As String, strLinkDSNname As String, strLinkLibName ... ' MsgBox "This database is in MDE format...I will delete/recreate ODBC ... Public Sub fncLinkDB2Table() ...
    (microsoft.public.access.modulesdaovba)
  • Re: OPENFILENAME A P I. User cancels ?
    ... The variable FName will be a Boolean False if no file was selected or a String containing the fully qualified file name if the user selectes a file. ... Const ahtOFN_OVERWRITEPROMPT = &H2 ... Function GetOpenFile(Optional varDirectory As Variant, ... Dim strFilter As String ...
    (microsoft.public.excel.programming)
  • RE: multi-select file dialog
    ... strCustomFilter As String ... Global Const ahtOFN_OVERWRITEPROMPT = &H2 ... Dim strFilter As String ... Function GetOpenFile(Optional varDirectory As Variant, ...
    (microsoft.public.access.modulesdaovba)
  • Re: Replace space in text file
    ... The reason I made vChar a variant (by simply not assigning any type to it to ... Dim sMarket As String ... case of vChars you have not specified the required variable type and VB ... standard variable length String which currently contained nine characters ...
    (microsoft.public.vb.general.discussion)
  • Re: Replace space in text file
    ... In four of the above cases you have specified which type of variable you want VB to create (Integer, Long, String or whatever). ... in the case of vChars you have not specified the required variable type and VB will therefore create it as a Variant. ... For example, if vChars was an Integer then VB would read two bytes from the file, and if it was a Long it would read four bytes, and if it was a standard variable length String which currently contained nine characters then it would read nine characters from the file and if it was a String that currently contained just one character then it would read one character from the file. ...
    (microsoft.public.vb.general.discussion)