Checking if a session object is null
-
This gem was on the asp.net forums:
if (Session["_serial"] + "" == "")
Response.Redirect("menu.aspx", true);Steve Wellens
At first I thought it looked ugly, but actually you can add a
null
to an empty string and the output will be an empty string. Also, adding two empty strings together produces an empty string, so what this code does is checks if the string Is Null Or Empty, very clever ;) -
This gem was on the asp.net forums:
if (Session["_serial"] + "" == "")
Response.Redirect("menu.aspx", true);Steve Wellens
If it's checking to see if the session object is null or empty, that seems perfectly cromulent.
-
At first I thought it looked ugly, but actually you can add a
null
to an empty string and the output will be an empty string. Also, adding two empty strings together produces an empty string, so what this code does is checks if the string Is Null Or Empty, very clever ;)Sam Cragg wrote:
At first I thought it looked ugly,
It is ugly.
Sam Cragg wrote:
very clever
Maintaining simple code is cheaper than maintaining 'clever' code.
// another way if (String.IsNullOrEmpty(Session\["\_serial"\] as String)) Response.Redirect("menu.aspx", true); // another way if ((Session\["\_serial"\] == null) || (Session\["\_serial"\] == "")) Response.Redirect("menu.aspx", true); // best way...now you can see the value of \_serial // and the logic is explicit and simple String \_serial = Session\["\_serial"\] as String; if (String.IsNullOrEmpty(\_serial)) Response.Redirect("menu.aspx", true);
Steve Wellens
modified on Thursday, September 16, 2010 9:17 PM
-
If it's checking to see if the session object is null or empty, that seems perfectly cromulent.
-
Cromulent Used in an ironical sense to mean legitimate, and therefore, in reality, spurious and not at all legitimate. Assumes common knowledge of the inherent Simpsons reference. Excellent choice of words, I have to remember this one... :-)
V.
Your prose will be the better for it. :)
-
Sam Cragg wrote:
At first I thought it looked ugly,
It is ugly.
Sam Cragg wrote:
very clever
Maintaining simple code is cheaper than maintaining 'clever' code.
// another way if (String.IsNullOrEmpty(Session\["\_serial"\] as String)) Response.Redirect("menu.aspx", true); // another way if ((Session\["\_serial"\] == null) || (Session\["\_serial"\] == "")) Response.Redirect("menu.aspx", true); // best way...now you can see the value of \_serial // and the logic is explicit and simple String \_serial = Session\["\_serial"\] as String; if (String.IsNullOrEmpty(\_serial)) Response.Redirect("menu.aspx", true);
Steve Wellens
modified on Thursday, September 16, 2010 9:17 PM
Sorry, I was trying to be sarcastic (the winking smiley?), as I said:
checks if the string Is Null Or Empty
Hinting at the built in function
string.IsNullOrEmpty
:doh: -
This gem was on the asp.net forums:
if (Session["_serial"] + "" == "")
Response.Redirect("menu.aspx", true);Steve Wellens
IIRC it's an idiom commonly used in classic ASP to get rid of unwanted nulls when getting data from ADODB Recordset (nulls are quite tricky to test for in ASP3):
field = rs("field") & ""
orfield = CLng("0" & rs("field"))
Seems like some programmer out there has some nostalgy...Luca The Price of Freedom is Eternal Vigilance. -- Wing Commander IV En Það Besta Sem Guð Hefur Skapað, Er Nýr Dagur. (But the best thing God has created, is a New Day.) -- Sigur Ròs - Viðrar vel til loftárása
-
IIRC it's an idiom commonly used in classic ASP to get rid of unwanted nulls when getting data from ADODB Recordset (nulls are quite tricky to test for in ASP3):
field = rs("field") & ""
orfield = CLng("0" & rs("field"))
Seems like some programmer out there has some nostalgy...Luca The Price of Freedom is Eternal Vigilance. -- Wing Commander IV En Það Besta Sem Guð Hefur Skapað, Er Nýr Dagur. (But the best thing God has created, is a New Day.) -- Sigur Ròs - Viðrar vel til loftárása
Luca Leonardo Scorcia wrote:
Seems like some programmer out there has some nostalgy...
It's not just nostalgia. If you move from a language that doesn't have a feature into one that does, it can take quite a while to "discover" that feature. You've got a solution that works. To my shame I only recently discovered (thanks to someone on CodeProject correcting me) that you can add methods to structs. I've been using them as simple data structures for so long that it never crossed my mind to see if I could add methods and constructors. -Rd
-
At first I thought it looked ugly, but actually you can add a
null
to an empty string and the output will be an empty string. Also, adding two empty strings together produces an empty string, so what this code does is checks if the string Is Null Or Empty, very clever ;)It is rather clever if it is dotNet1.1 where string.IsNullorEmpty doesn't exist. I would probably suggest that a dev write it in a more legible way though so that their code wouldn't end up here.
-
This gem was on the asp.net forums:
if (Session["_serial"] + "" == "")
Response.Redirect("menu.aspx", true);Steve Wellens
If this sort of thing was implemented as a function, and I mean a VERY well-documented function, it would be ingenuous (especially if it was inline, where this is permitted). Otherwise, cute constructs like this are a pain in the tailbone unless you only hire coders who instantly recognize every trick in the book. Good luck with that :laugh: