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

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

问题描述

if(IsChNoSpace >= 48 & IsChNoSpace <= 57)



尝试使用此条件时显示以下错误




am try to use this condition it shows the follwing error

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





请帮帮我



Please help me

推荐答案

这个错误给你声明IsChNoSpace是字符串。所以你要检查条件你试试这个

this error given you for you declare IsChNoSpace is string. so you want to check with if condition you try this
if(int.Parse(IsChNoSpace)>= 48 && int.Parse(IsChNoSpace)<= 57)
{

}





你可以使用代码



char.IsLetterOrDigit(IsChNoSpace) //这假设IsChNoSpace的类型为char



来测试你的角色是否是一封信,而不是其他任何东西。这将考虑到Caps和小写字母。不仅仅是你的例子中的上限。
Hi
you could use the code

char.IsLetterOrDigit(IsChNoSpace) // This is assuming that IsChNoSpace is of type char

to test if your character is a letter and not anything else. This will take into account Caps and small letters though. Not only caps as in your example.


如果 IsChNoSpace 变量是一个字符串或一个char,你必须这样做:< br $> b $ b

If the IsChNoSpace variable is a string or a char, you have to do this:

int x;
if (ToInt32.TryParse(IsChNoSpace, out x))
{
    if (x >= 48 && x <= 57)
    {
        // ...do something
    }
}



除此之外,你在原始比较中使用了错误的运算符:




Beyond that, you're using the wrong operator in your orginal comparison:

if (IsChNoSpace >= 48 & IsChNoSpace <= 57)





应该是





should be

if (IsChNoSpace >= 48 && IsChNoSpace <= 57)





编辑(关于您选择的运营商)==================== ===



在这个例子中,是的,你是对的,因为你只是在比较内在价值。但是,如果你这样做:





EDIT (regarding your choice of operators) =======================

In this instance, yes, you're right, because you're simply comparing intrinsic values. HOWEVER, if you are doing something like this:

if (object != null & object.XYZ < 30)





... C#将在object上抛出一个空引用异常,因为它会计算BOTH表达式,第二个会引发异常)。在此版本的相同代码中:





...C# will throw a null reference exception on "object" because it evaluates BOTH expressions, and the second one will throw the exception). In this version of the same code:

if (object != null && object.XYZ < 30)





...你不会得到例外。



只因为你 CAN 做某事,并不代表你 应该 做那件事。让我感到惊讶的是,使用按位运算符,其中预期逻辑运算符不会导致编译器生成警告。



为什么这是不好的做法?因为它不符合标准。它会在理解代码时产生问题,因为您在最初编写代码时没有对代码进行注释。如果你正在开展一个项目我是主角,并且在代码审查期间发现了这种事情,我会要求它被更改,并且你会在你的人事档案中得到正式的谴责。是的 - 我是个笨蛋。



...you will NOT get the exception.

Just because you CAN do something, doesn't mean you SHOULD do that something. What surprises me the most is that using a bitwise operator where a logical one is expected doesn't cause the compiler to generate a warning.

Why is it bad practice? because it's NOT STANDARD. It creates problems where understanding the code is concerned because you didn't comment the code when you originally wrote it. If you were working on a project I was the lead on, and this kind of thing was discovered during a code review, I would demand that it be changed, and you'd get an official reprimand in your personnel file. And yeah - I'm a hard-ass.


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

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