views:

133

answers:

7

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?

A: 

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.

Douglas
A: 

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"];
}
danijels
This wouldn't compile because you're incorrectly assuming that the `HttpSessionState[ string ]` indexer is going to return an `HttpSessionState` object where it actually only returns an `object`
Dave
+2  A: 
    object ss = HttpContext.Current.Session["pdfDocument"] ?? false; 
    if ((Boolean)ss) 
    { 
        Label1.Text = (String)Session["docName"]; 
    } 
Akash Kava
+3  A: 

Why do you use ss variable?

What about this:

if (HttpContext.Current.Session["pdfDocument"] != null)
{
    Label1.Text = (String)Session["docName"];
}
bniwredyc
I ditched the code above and went with this, many thanks
m.edmondson
@eddy556, you're welcome
bniwredyc
+1  A: 

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.

Tommy
I'm pretty sure that this wouldn't compile because you've declared `ss` as an `HttpSessionState` object and you're trying to null coalesce that type with an incompatible `bool` type.
Dave
Sorry, I have my operator precedence way off. As '=' is lower precedence than '??' the ss = HttpContext.Current.Session["pdfDocument"] needs an extra set of brackets. As in: (Boolean)((ss = HttpContext.Current.Session["pdfDocument"]) ?? false)
Tommy
A: 

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;
Ioannis Karadimas
`bool.TryParse`'s first argument accepts a `string`, not an `object` which is returned by the `HttpSessionState[ string ]` indexer. Also, even if you were to attempt to cast the value at "pdfDocument" to a string you would be making the assumption that you would be storing a string value that `bool.TryParse` can parse.
Dave
You 're right about the object part. However, it is precisely because I don't want to assume it can be parsed that I use TryParse(), not Parse().
Ioannis Karadimas
by adding `ToString()` now you're going get a `NullReferenceException` if there is nothing at that location in the Session.
Dave
Right yet again. Thanks for pointing it out...
Ioannis Karadimas
If the value at "pdfDoucment" is a `bool` this will still always be false `bool baddness() { bool parsed; bool.TryParse(((object)true) as string, out parsed); return parsed; }` will always return `false`.
Dave
I guess I got carried away on this lost cause... Thanks 4 taking the time with me.
Ioannis Karadimas
A: 

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

Dave