Click here to Skip to main content
15,888,579 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
hi all,



C++
string[] result=new string [4];
      string accessnumber = null;
      accessnumber = "";
      result = clsObj.FnCheckAccessNo();
      if (result[0] == 1)
      {
          accessnumber = result[2];
      }


Am try to use this condition it shows the follwing error, please let me know if anybody knows..

Operator '==' cannot be applied to operands of type 'string' and 'int'



Source Error:

Line 160: accessnumber = "";
Line 161: result = clsObj.FnCheckAccessNo();
Line 162: if (result[0] == 1)
Line 163: {
Line 164: accessnumber = result[2];
Posted
Updated 27-Sep-12 2:23am
v2
Comments
Legor 27-Sep-12 9:02am    
Please accept one of the answers below.

As result is an array of strings, result[0] is a string. 1 is an int literal. It is normal, that you can not compare them.
What about: if (result[0] == "1") ?
 
Share this answer
 
v2
There is not much to add since the error message allready says it all...
You can no compare a String with an Integer using the == operator.
What is it you want to achieve?

You can compare two numbers like
C++
int a = 0;
if (a == 0) ...


or two Strings
C++
string word  = "Hello";
if (word == "Bark") ...


but not
C++
if (a == word)
...

Maybe now you see what is wrong in your code?
 
Share this answer
 
v2
C#
if (result[0] == (1).ToString())

or
C#
if (result[0] == "1")

Happy Coding!
:)
 
Share this answer
 
Hello,


Please try below corrected one :

C#
string[] result=new string [4];
            string accessnumber = null;
            accessnumber = "";
            result = clsObj.FnCheckAccessNo();
            if ( Convert.ToString(result[0]) == Convert.ToString(1))
            {
                accessnumber = result[2];
            }



Please let us know if any.
 
Share this answer
 
use following code
Put 1 inside "" or use 1.tostring()

if (result[0] == "1")

or

if (result[0] == 1.toString())
 
Share this answer
 
you cant compare different datatypes
so convert any comparing datatype

C#
if (result[0] == Convert.ToString(1))
  {
    accessnumber = result[2];
  }
 
Share this answer
 
Cast right side string to integer if function returns numeric values

C#
string[] result=new string [4];
            string accessnumber = null;
            accessnumber = "";
            result = clsObj.FnCheckAccessNo();
            if ( int.parse(result[0]) == 1)
            {
                accessnumber = result[2];
            }
 
Share this answer
 
To compare string you can use
C#
string.Compare(string s1)

In C# :D
 
Share this answer
 
v2
you can use
result[0].Equals("1")


Regards,
Bhushan Shah
 
Share this answer
 
If you wants compare two string then use

C#
a==b


and if u wants to assign the value use

C#
a=b
 
Share this answer
 
v2
Comments
Legor 27-Sep-12 8:34am    
There is a mistake in your solution: "a===b"
stellus 27-Sep-12 8:35am    
thanks for give me a drop of solutions , now its working fine...
wings_ 27-Sep-12 8:40am    
ok thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900