如何在ASP.NET中使用&&(运算符)类型为bool或int的操作数 [英] How to use &&(operator) on operands of type bool or int in ASP.NET

查看:232
本文介绍了如何在ASP.NET中使用&&(运算符)类型为bool或int的操作数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的ASP.NET代码中的if condition中使用&&(和)运算符。

我正在应用的两个操作数&& operator是int还是int?分别。

但它显示错误,你不能申请&& int上的运算符。我怎么能解决这个错误,因为我想使用&&运营商。请帮帮我。



我的尝试:



if( temp< = 6){

if(check == 1&& h = Convert.ToInt32(TextBox1.Text))

{

Label1.Text =抱歉,这个群体已经限制了它的限制;



}

I want to use &&(and) operator in the "if condition" in my code of ASP.NET.
My both operands on which i am applying && operator are int and int? respectively.
But it is showing error that, you can not apply && operator on int . How i can solve that error because i want to use && operator. Please help me.

What I have tried:

if(temp<=6){
if (check==1 && h=Convert.ToInt32(TextBox1.Text))
{
Label1.Text = "Sorry That group has corss its limits";

}

推荐答案

你没有在int上应用操作数,你只在bools上应用它们。



如果仔细观察第二个操作数,你可以清楚地看到问题。您将Convert.ToInt32的值分配给h是什么。
You are not applying the operands on an int, you apply them only on bools.

If you look close at your second operand you can see the issue clearly. You are assigning the value of Convert.ToInt32 to whatever h is.


这是一个有点误导性的错误消息。 真正的问题是你在第二个条件中缺少 =

That's a slightly misleading error message. The real problem is that you're missing an = in your second condition:
h == Convert.ToInt32(TextBox1.Text)



只有一个 = ,你就是将 h 的值设置为 TextBox1 中的值,而不是将其比较。



注意:您应该使用 Int32.TryParse [ ^ ]而不是 Convert.ToInt32 ,以避免在用户输入的内容时出现异常无法转换为整数。


With only a single =, you're setting the value of h to the value from TextBox1, rather than comparing it.

NB: You should probably use Int32.TryParse[^] instead of Convert.ToInt32, to avoid getting an exception if the user enters something that can't be converted to an integer.


这篇关于如何在ASP.NET中使用&amp;&amp;(运算符)类型为bool或int的操作数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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