运算符'=='不能应用于类型为'string'和'int'的操作数 [英] Operator '==' cannot be applied to operands of type 'string' and 'int'

查看:86
本文介绍了运算符'=='不能应用于类型为'string'和'int'的操作数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



hi all,



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



我尝试使用这种情况来显示以下错误,如果有人知道,请告诉我..

运算符"=="不能应用于类型为"string"和"int"的操作数



源错误:

第160行:accessnumber =";
第161行:result = clsObj.FnCheckAccessNo();
第162行: 如果(结果[0] == 1)
第163行:{
第164行: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];

推荐答案

由于result是一个字符串数组,因此result [0]是一个字符串. 1是int文字.很正常,您无法比较它们.
怎么回事:if (result[0] == "1")?
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") ?


由于错误消息已经全部说明了,所以没有什么可添加的.
您不能使用==运算符将字符串与整数进行比较.
您想要实现什么?

您可以比较两个数字,例如
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
int a = 0;
if (a == 0) ...



或两个字符串



or two Strings

string word  = "Hello";
if (word == "Bark") ...



但不是



but not

if (a == word)

...

也许现在您看到代码中的问题了吗?

...

Maybe now you see what is wrong in your code?


if (result[0] == (1).ToString())




or

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


祝您编码愉快!
:)


Happy Coding!
:)


这篇关于运算符'=='不能应用于类型为'string'和'int'的操作数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆