开发者

Using the conditional operator ? to check for null session variable

开发者 https://www.devze.com 2023-01-19 16:28 出处:网络
Take a look at this code: System.Web.SessionState.HttpSessionState ss = HttpContext.Current.Session[\"pdfDocument\"] ?? f开发者_如何学JAVAalse;

Take a look at this code:

System.Web.SessionState.HttpSessionState ss = HttpContext.Current.Session["pdfDocument"] ?? f开发者_如何学JAVAalse;

        if ((Boolean)ss)
        {
            Label1.Text = (String)Session["docName"];
        }

Basically I want to check if HttpContext.Current.Session["pdfDocument"] is not null, and if it isn't to cast to Boolean, then check if its true or false.

I'm trying to avoid nested if statements and figured there would be a more elegant way to do it. I'm therefore only interested in answers that contain the conditional ? operator.

Any tips?


Why do you use ss variable?

What about this:

if (HttpContext.Current.Session["pdfDocument"] != null)
{
    Label1.Text = (String)Session["docName"];
}


    object ss = HttpContext.Current.Session["pdfDocument"] ?? false; 
    if ((Boolean)ss) 
    { 
        Label1.Text = (String)Session["docName"]; 
    } 


Not sure exactly what you're asking for, how about:

System.Web.SessionState.HttpSessionState ss;

Label1.Text = (Boolean)((ss = HttpContext.Current.Session["pdfDocument"]) ?? false) ? (String)Session["docName"] : Label1.Text;

Should leave ss with either a valid session or null, avoids the problem of trying to store false to ss and completely skips the subsequent 'if'. Though there's a repetition of Label1.Text.

Note: this has been edited to take account of the comment by Dave below.


The problem is that you can't do this:

SessionState.HttpSessionState ss = false;

Try putting your nested ifs into an extension method then call that instead.


HttpContext.Current.Session is an System.Web.SessionState.HttpSessionState object, which is a hash or dictionary, as some may call it, of different objects, so unless you're storing an HttpSessionState object as the "pdfDocument" location the first line is incorrect.

If you're actually storing a bool in the "pdfDocument" location which may or may not already be in this slot, you could cast that directly to a bool and null coalesce it: var ss = (bool)(HttpContext.Current.Session["pdfDocument"] ?? false);.

If you're possibly storing some other kind of object in the "pdfDocument" location you could just see if it's currently at that location by checking for null: var ss = HttpContext.Current.Session["pdfDocument"] != null;.


You can try this, though I don't know if it fits your aesthetics:

bool isPdfDocumentSet =
     bool.TryParse((HttpContext.Current.Session["pdfDocument"] as string, 
         out isPdfDocumentSet)
             ? isPdfDocumentSet
             : false;

EDIT: There is actually an even more concise way of doing it:

bool isPdfDocumentSet =
     bool.TryParse(HttpContext.Current.Session["pdfDocument"] as string, 
          out isPdfDocumentSet) && isPdfDocumentSet;


I think the closest you will get to the solution taking that path is following:

System.Web.SessionState.HttpSessionState ss = HttpContext.Current.Session["pdfDocument"];
if (ss != null)
{
    Label1.Text = (String)Session["docName"];
}
0

精彩评论

暂无评论...
验证码 换一张
取 消