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. How do you like your nulls

How do you like your nulls

Scheduled Pinned Locked Moved The Weird and The Wonderful
questioncode-review
19 Posts 11 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.
  • R Robert Hoffmann

    In a project i had to refactor: if (!string.isNullOrEmpty(User.ToString())) { } null.ToString() ?? sweet :-D

    L Offline
    L Offline
    lewax00
    wrote on last edited by
    #3

    I'm not sure I get what's wrong here with the given amount of context (except that ToString probably shouldn't return null...but it certainly could if it's overloaded) :doh:

    B B 2 Replies Last reply
    0
    • L lewax00

      I'm not sure I get what's wrong here with the given amount of context (except that ToString probably shouldn't return null...but it certainly could if it's overloaded) :doh:

      B Offline
      B Offline
      Brisingr Aerowing
      wrote on last edited by
      #4

      ToString is an instance method, and if the user variable is null, there will be an unhandled NullReferenceException when the ToString() is called, as you cannot call a method on a null object.

      Bob Dole

      The internet is a great way to get on the net.

      :doh: 2.0.82.7292 SP6a

      L T L 3 Replies Last reply
      0
      • B Brisingr Aerowing

        ToString is an instance method, and if the user variable is null, there will be an unhandled NullReferenceException when the ToString() is called, as you cannot call a method on a null object.

        Bob Dole

        The internet is a great way to get on the net.

        :doh: 2.0.82.7292 SP6a

        L Offline
        L Offline
        lewax00
        wrote on last edited by
        #5

        Right, but there's no reason to expect it might be null at this piece of code without more context. The previous line could be something that gets that user from a function that always returns an non-null object...I just don't think there's enough information to comment on the code as-is.

        R 1 Reply Last reply
        0
        • L lewax00

          Right, but there's no reason to expect it might be null at this piece of code without more context. The previous line could be something that gets that user from a function that always returns an non-null object...I just don't think there's enough information to comment on the code as-is.

          R Offline
          R Offline
          Robert Hoffmann
          wrote on last edited by
          #6

          I understand your logic, but User can be null. But either way turned ToString in this situation is also completely redundant because IsNullOrEmpty already does 2 specific checks. Is the item null, or is the item an empty string. So the correct code, without triggering a NullReferenceException should have been if (!string.IsNullOrEmpty(User)) { } :thumbsup:

          L 1 Reply Last reply
          0
          • R Robert Hoffmann

            I understand your logic, but User can be null. But either way turned ToString in this situation is also completely redundant because IsNullOrEmpty already does 2 specific checks. Is the item null, or is the item an empty string. So the correct code, without triggering a NullReferenceException should have been if (!string.IsNullOrEmpty(User)) { } :thumbsup:

            L Offline
            L Offline
            lewax00
            wrote on last edited by
            #7

            Oh is User a string? If so I get it now...

            R 1 Reply Last reply
            0
            • L lewax00

              Oh is User a string? If so I get it now...

              R Offline
              R Offline
              Robert Hoffmann
              wrote on last edited by
              #8

              Yeah it's a string and probably read UserName originally. Sorry for the confusion :-\

              P 1 Reply Last reply
              0
              • L lewax00

                I'm not sure I get what's wrong here with the given amount of context (except that ToString probably shouldn't return null...but it certainly could if it's overloaded) :doh:

                B Offline
                B Offline
                BobJanova
                wrote on last edited by
                #9

                Returning null from a ToString would be worthy of a WTF of its own.

                1 Reply Last reply
                0
                • B Brisingr Aerowing

                  ToString is an instance method, and if the user variable is null, there will be an unhandled NullReferenceException when the ToString() is called, as you cannot call a method on a null object.

                  Bob Dole

                  The internet is a great way to get on the net.

                  :doh: 2.0.82.7292 SP6a

                  T Offline
                  T Offline
                  Tomz_KV
                  wrote on last edited by
                  #10

                  You are correct. That is the existing implementation (or null handling). In my opinion, it would be nice to allow the conversion (.ToString()) from null to an empty string. When something is nothing (null), it is logical to think it is an empty in terms of a string. However, this ertainly not applicable to other data types.

                  TOMZ_KV

                  B 1 Reply Last reply
                  0
                  • R Robert Hoffmann

                    In a project i had to refactor: if (!string.isNullOrEmpty(User.ToString())) { } null.ToString() ?? sweet :-D

                    R Offline
                    R Offline
                    RafagaX
                    wrote on last edited by
                    #11

                    i prefer:

                    if(User != null && User.ToString() != "")

                    ;P

                    CEO at: - Rafaga Systems - Para Facturas - Modern Components for the moment...

                    J 1 Reply Last reply
                    0
                    • R Robert Hoffmann

                      Yeah it's a string and probably read UserName originally. Sorry for the confusion :-\

                      P Offline
                      P Offline
                      patbob
                      wrote on last edited by
                      #12

                      Calling ToString() on a string is totally unexpected. Perfectly valid code, but I'd wonder about the programmer who wrote it. How that line ever got written in the first place made a lot more sense when I was thinking User wasn't a string -- you can't pass a non-string to IsNullOrEmpty(). Still broke, but I can see someone making such a mistake if in a hurry. Any chance it wasn't a string at one time?

                      We can program with only 1's, but if all you've got are zeros, you've got nothing.

                      1 Reply Last reply
                      0
                      • R Robert Hoffmann

                        In a project i had to refactor: if (!string.isNullOrEmpty(User.ToString())) { } null.ToString() ?? sweet :-D

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

                        Maybe they have CDO?

                        if (!string.IsNullOrEmpty(User.ToString().ToString().ToString().ToString())) {
                        // OK, now I can be sure it's a string. It's a string. A string. String.
                        }

                        Thou mewling ill-breeding pignut!

                        B 1 Reply Last reply
                        0
                        • T Tomz_KV

                          You are correct. That is the existing implementation (or null handling). In my opinion, it would be nice to allow the conversion (.ToString()) from null to an empty string. When something is nothing (null), it is logical to think it is an empty in terms of a string. However, this ertainly not applicable to other data types.

                          TOMZ_KV

                          B Offline
                          B Offline
                          Brisingr Aerowing
                          wrote on last edited by
                          #14

                          True. That would be useful.

                          Bob Dole

                          The internet is a great way to get on the net.

                          :doh: 2.0.82.7292 SP6a

                          1 Reply Last reply
                          0
                          • A AspDotNetDev

                            Maybe they have CDO?

                            if (!string.IsNullOrEmpty(User.ToString().ToString().ToString().ToString())) {
                            // OK, now I can be sure it's a string. It's a string. A string. String.
                            }

                            Thou mewling ill-breeding pignut!

                            B Offline
                            B Offline
                            Brisingr Aerowing
                            wrote on last edited by
                            #15

                            CDO. It's like OCD, just in alphabetical order like it should be!

                            Bob Dole

                            The internet is a great way to get on the net.

                            :doh: 2.0.82.7292 SP6a

                            1 Reply Last reply
                            0
                            • B Brisingr Aerowing

                              ToString is an instance method, and if the user variable is null, there will be an unhandled NullReferenceException when the ToString() is called, as you cannot call a method on a null object.

                              Bob Dole

                              The internet is a great way to get on the net.

                              :doh: 2.0.82.7292 SP6a

                              L Offline
                              L Offline
                              Lost User
                              wrote on last edited by
                              #16

                              Zac Greve wrote:

                              ToString is an instance method

                              Replace it with an extension-method :)

                              Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] They hate us for our freedom![^]

                              1 Reply Last reply
                              0
                              • R Robert Hoffmann

                                In a project i had to refactor: if (!string.isNullOrEmpty(User.ToString())) { } null.ToString() ?? sweet :-D

                                F Offline
                                F Offline
                                Florin Jurcovici 0
                                wrote on last edited by
                                #17

                                I like my nulls to be null objects.

                                1 Reply Last reply
                                0
                                • R RafagaX

                                  i prefer:

                                  if(User != null && User.ToString() != "")

                                  ;P

                                  CEO at: - Rafaga Systems - Para Facturas - Modern Components for the moment...

                                  J Offline
                                  J Offline
                                  Jibesh
                                  wrote on last edited by
                                  #18

                                  how about this. if( User != null && User.Length != 0) is better than checking for empty string ??? :rolleyes:

                                  Jibesh.V.P India

                                  R 1 Reply Last reply
                                  0
                                  • J Jibesh

                                    how about this. if( User != null && User.Length != 0) is better than checking for empty string ??? :rolleyes:

                                    Jibesh.V.P India

                                    R Offline
                                    R Offline
                                    RafagaX
                                    wrote on last edited by
                                    #19

                                    :doh: Why didn't i though it?

                                    CEO at: - Rafaga Systems - Para Facturas - Modern Components for the moment...

                                    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