运算符 '||' 不能应用于类型为 'bool' 和 'int' 的操作

本文关键字:int 操作 bool 应用于 不能 运算符 类型 | 更新日期: 2023-09-27 18:35:49

我想使用"if"循环检查整数值是否为零或其他整数。我做过这样的事

 int ID = (int)Session["id"];//I have assigned Session["id"]=20; on previous page
    if (ID == 0 || ID =50)
        Response.Redirect("Login.aspx");

但它显示错误运算符 '||' 不能应用于类型为 'bool' 和 'int' 的操作数。请帮忙

运算符 '||' 不能应用于类型为 'bool' 和 'int' 的操作

您正在将 50 分配给 ID。不比较...它应该是

if (ID == 0 || ID ==50)

更正你的 if 块

if (ID == 0 || ID ==50)
    Response.Redirect("Login.aspx");

你想要这个:-

if (ID == 0 || ID ==50)
                   ^---> //Change here as you are not comparing you are assigning which is not correct as per your need

因为=表示分配,您想进行比较。因此,您可能需要使用==而不是=

你在 if 块中使用了"="而不是"=="

 if (ID == 0 || ID == 50)
            Response.Redirect("Login.aspx");

我想你错过了那里的==

请在下面尝试。

int ID = (int)Session["id"];//I have assigned Session["id"]=20; on previous page
    if (ID == 0 || ID == 50)
        Response.Redirect("Login.aspx");