Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. The Weird and The Wonderful
  4. Checking if a session object is null

Checking if a session object is null

Scheduled Pinned Locked Moved The Weird and The Wonderful
csharprubyasp-net
11 Posts 8 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Steve Wellens

    This gem was on the asp.net forums:

    if (Session["_serial"] + "" == "")
    Response.Redirect("menu.aspx", true);

    Steve Wellens

    S Offline
    S Offline
    Samuel Cragg
    wrote on last edited by
    #2

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

    S F 2 Replies Last reply
    0
    • S Steve Wellens

      This gem was on the asp.net forums:

      if (Session["_serial"] + "" == "")
      Response.Redirect("menu.aspx", true);

      Steve Wellens

      A Offline
      A Offline
      AspDotNetDev
      wrote on last edited by
      #3

      If it's checking to see if the session object is null or empty, that seems perfectly cromulent.

      [Forum Guidelines]

      V 1 Reply Last reply
      0
      • S Samuel Cragg

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

        S Offline
        S Offline
        Steve Wellens
        wrote on last edited by
        #4

        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

        S 1 Reply Last reply
        0
        • A AspDotNetDev

          If it's checking to see if the session object is null or empty, that seems perfectly cromulent.

          [Forum Guidelines]

          V Offline
          V Offline
          V 0
          wrote on last edited by
          #5

          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.

          A 1 Reply Last reply
          0
          • V V 0

            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.

            A Offline
            A Offline
            AspDotNetDev
            wrote on last edited by
            #6

            Your prose will be the better for it. :)

            [Forum Guidelines]

            1 Reply Last reply
            0
            • S Steve Wellens

              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

              S Offline
              S Offline
              Samuel Cragg
              wrote on last edited by
              #7

              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:

              1 Reply Last reply
              0
              • S Steve Wellens

                This gem was on the asp.net forums:

                if (Session["_serial"] + "" == "")
                Response.Redirect("menu.aspx", true);

                Steve Wellens

                L Offline
                L Offline
                Luca Leonardo Scorcia
                wrote on last edited by
                #8

                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") & "" or field = 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

                R 1 Reply Last reply
                0
                • L Luca Leonardo Scorcia

                  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") & "" or field = 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

                  R Offline
                  R Offline
                  Richard A Dalton
                  wrote on last edited by
                  #9

                  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

                  1 Reply Last reply
                  0
                  • S Samuel Cragg

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

                    F Offline
                    F Offline
                    fjdiewornncalwe
                    wrote on last edited by
                    #10

                    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.

                    1 Reply Last reply
                    0
                    • S Steve Wellens

                      This gem was on the asp.net forums:

                      if (Session["_serial"] + "" == "")
                      Response.Redirect("menu.aspx", true);

                      Steve Wellens

                      C Offline
                      C Offline
                      cpkilekofp
                      wrote on last edited by
                      #11

                      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:

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups