m.edmondson
m.edmondson

Reputation: 30922

Using the conditional operator ? to check for null session variable

Take a look at this code:

System.Web.SessionState.HttpSessionState ss = HttpContext.Current.Session["pdfDocument"] ?? false;

        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?

Upvotes: 3

Views: 3583

Answers (7)

Tommy
Tommy

Reputation: 100662

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.

Upvotes: 1

Ioannis Karadimas
Ioannis Karadimas

Reputation: 7906

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;

Upvotes: 0

David
David

Reputation: 19707

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;.

Upvotes: 0

bniwredyc
bniwredyc

Reputation: 8839

Why do you use ss variable?

What about this:

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

Upvotes: 4

Akash Kava
Akash Kava

Reputation: 39956

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

Upvotes: 2

danijels
danijels

Reputation: 5291

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"];
}

Upvotes: -1

Douglas
Douglas

Reputation: 37771

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.

Upvotes: 0

Related Questions