【问题标题】:Checking multiple session variables if they are null检查多个会话变量是否为空
【发布时间】:2013-05-06 12:25:00
【问题描述】:

我正在使用以下方法检查一些会话变量:

if(MySession.Current.mpr != null && MySession.Current.mpr1 != null && MySession.Current.mpr2 != null
  && MySession.Current.mpr3 != null && MySession.Current.mip != null && MySession.Current.vr != null)
{
  ....
}

它不起作用!我知道其中一个变量不为空。 有什么建议吗?

【问题讨论】:

  • 调试的时候有没有发现哪个变量不为空?
  • 是的,它是 MySession.Current.mip。

标签: c# asp.net-mvc session session-variables


【解决方案1】:

您的if 语句仅在所有变量都不是null 时才进入块内部。如果一个变量不为空,要进入,请使用 ors || 而不是和 &&:

if(MySession.Current.mpr != null || MySession.Current.mpr1 != null || MySession.Current.mpr2 != null
  || MySession.Current.mpr3 != null || MySession.Current.mip != null || MySession.Current.vr != null)
{
  ....
}

【讨论】:

    猜你喜欢
    • 2018-04-24
    • 1970-01-01
    • 2011-11-02
    • 1970-01-01
    • 1970-01-01
    • 2012-02-05
    相关资源
    最近更新 更多