错误:运算符'!'不能应用于类型为object的操作数 [英] Error: Operator '!' cannot be applied to operand of type object

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

问题描述

我有一个错误显示:在我的以下代码中,运算符!"无法应用于类型为object的操作数.我该如何解决?

I have a error that shows: Operator ''!'' cannot be applied to operand of type object in my following code. How do i solve it?

if (!(Session["counter"]))
        {
            counter = (int)Session["counter"];
        }

推荐答案

尝试一下.

Try this.

(! (Session["Counter"] is null)
counter = (int)Session["counter"];


MSDN [ ^ ]状态:
>
逻辑否定运算符(!)是一元运算符,它会否定其操作数. bool 定义,并且仅当其操作数为false时才返回true.


The logical negation operator (!) is a unary operator that negates its operand. It is defined for bool and returns true if and only if its operand is false.


因此您不能将其应用于整数变量.

您可以将其更改为:


hence you cannot apply it to integer variables.

You might change it to:

if ( Session["counter"] != 0)
{
//...
}


这取决于会话变量Counter的值.

如果您假设它是一个integer 值,那么您应该编写类似
的代码
It depends on the value of the session variable Counter.

If you are assuming it is a integer value, you should be writing code like
if (!(int)Session["counter"] == 0)
{
  counter = ....
}


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

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