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. General Programming
  3. Visual Basic
  4. Testing if a varible is null

Testing if a varible is null

Scheduled Pinned Locked Moved Visual Basic
testingbeta-testinghelpquestion
13 Posts 7 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 Offline
    R Offline
    RachelSo
    wrote on last edited by
    #1

    My original code looked like this:

    If strNumber <> vbNull And strProvider <> vbNull And strEmail <> vbNull Then

    But that would throw an error @ strProvider saying that i was trying to convert a string to a double. So I changed it to:

    If strNumber <> vbNull & strProvider <> vbNull & strEmail <> vbNull Then

    (replaced "And" with '&') Now it throws an error @ strEmail saying that im trying to convert string strEmail to a Boolean. Can someone please tell me what Im doing wrong?

    J D D R 4 Replies Last reply
    0
    • R RachelSo

      My original code looked like this:

      If strNumber <> vbNull And strProvider <> vbNull And strEmail <> vbNull Then

      But that would throw an error @ strProvider saying that i was trying to convert a string to a double. So I changed it to:

      If strNumber <> vbNull & strProvider <> vbNull & strEmail <> vbNull Then

      (replaced "And" with '&') Now it throws an error @ strEmail saying that im trying to convert string strEmail to a Boolean. Can someone please tell me what Im doing wrong?

      J Offline
      J Offline
      jtpaa
      wrote on last edited by
      #2

      What if You replace the vbNull with Nothing?

      1 Reply Last reply
      0
      • R RachelSo

        My original code looked like this:

        If strNumber <> vbNull And strProvider <> vbNull And strEmail <> vbNull Then

        But that would throw an error @ strProvider saying that i was trying to convert a string to a double. So I changed it to:

        If strNumber <> vbNull & strProvider <> vbNull & strEmail <> vbNull Then

        (replaced "And" with '&') Now it throws an error @ strEmail saying that im trying to convert string strEmail to a Boolean. Can someone please tell me what Im doing wrong?

        D Offline
        D Offline
        DaveAuld
        wrote on last edited by
        #3

        if you want to test if the string is empty use if strNumber.Equals(string.empty), if you want to test the object is an actual object use if strNumber isNot Nothing So to test the list you have above;

        If (strNumber isnot nothing) and (strProvider isNot Nothing) and (strEmail isNot nothing) then

        or if you are testing for empty string i.e "" then

        if strnumber.equals(string.empty) and strProvider.equals(string.empty) and strEmail.equals(string.empty) then

        Dave Don't forget to rate messages! Find Me On: Web|Facebook|Twitter|LinkedIn Waving? dave.m.auld[at]googlewave.com

        D 1 Reply Last reply
        0
        • D DaveAuld

          if you want to test if the string is empty use if strNumber.Equals(string.empty), if you want to test the object is an actual object use if strNumber isNot Nothing So to test the list you have above;

          If (strNumber isnot nothing) and (strProvider isNot Nothing) and (strEmail isNot nothing) then

          or if you are testing for empty string i.e "" then

          if strnumber.equals(string.empty) and strProvider.equals(string.empty) and strEmail.equals(string.empty) then

          Dave Don't forget to rate messages! Find Me On: Web|Facebook|Twitter|LinkedIn Waving? dave.m.auld[at]googlewave.com

          D Offline
          D Offline
          David Skelly
          wrote on last edited by
          #4

          If it's VB.NET you can also combine them in one:

          String.IsNullOrEmpty(strNumber)

          Possibly not relevant to the original question, but useful to know.

          1 Reply Last reply
          0
          • R RachelSo

            My original code looked like this:

            If strNumber <> vbNull And strProvider <> vbNull And strEmail <> vbNull Then

            But that would throw an error @ strProvider saying that i was trying to convert a string to a double. So I changed it to:

            If strNumber <> vbNull & strProvider <> vbNull & strEmail <> vbNull Then

            (replaced "And" with '&') Now it throws an error @ strEmail saying that im trying to convert string strEmail to a Boolean. Can someone please tell me what Im doing wrong?

            D Offline
            D Offline
            David Skelly
            wrote on last edited by
            #5

            The problem here is that vbNull is not the same as Null. vbNull is an integer value. It is the code value which represents a variant type of null. So,

            VarType(Null) = vbNull

            If this is VB6 you check for a null variant value with:

            IsNull(aValue)

            If it is VB.NET, as mentioned elsewhere you should use

            strNumber IsNot Nothing

            or, if you are still using an older version of .NET you will have to turn it round and say

            Not (strNumber Is Nothing)

            since the IsNot keyword was introduced in .NET 2 (I think). Also, the & operator does something completely different to the And keyword. You can't simply replace one with the other.

            D 1 Reply Last reply
            0
            • D David Skelly

              The problem here is that vbNull is not the same as Null. vbNull is an integer value. It is the code value which represents a variant type of null. So,

              VarType(Null) = vbNull

              If this is VB6 you check for a null variant value with:

              IsNull(aValue)

              If it is VB.NET, as mentioned elsewhere you should use

              strNumber IsNot Nothing

              or, if you are still using an older version of .NET you will have to turn it round and say

              Not (strNumber Is Nothing)

              since the IsNot keyword was introduced in .NET 2 (I think). Also, the & operator does something completely different to the And keyword. You can't simply replace one with the other.

              D Offline
              D Offline
              David Skelly
              wrote on last edited by
              #6

              Sudden thought: if it's VB6 you may also need to read up on the difference between IsNull and IsEmpty.

              D 1 Reply Last reply
              0
              • D David Skelly

                Sudden thought: if it's VB6 you may also need to read up on the difference between IsNull and IsEmpty.

                D Offline
                D Offline
                DaveAuld
                wrote on last edited by
                #7

                I thought the new ruling on the VB board was assume .Net unless the OP tells you differently, that way [vb6 users] will be bomarded with all the .net goodness, and have to admit they are using VB6 and realise its maybe time to move on.

                Dave Don't forget to rate messages! Find Me On: Web|Facebook|Twitter|LinkedIn Waving? dave.m.auld[at]googlewave.com

                L 1 Reply Last reply
                0
                • D DaveAuld

                  I thought the new ruling on the VB board was assume .Net unless the OP tells you differently, that way [vb6 users] will be bomarded with all the .net goodness, and have to admit they are using VB6 and realise its maybe time to move on.

                  Dave Don't forget to rate messages! Find Me On: Web|Facebook|Twitter|LinkedIn Waving? dave.m.auld[at]googlewave.com

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #8

                  that would be good, if only it got displayed on top of this forum so all can see it. :)

                  Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                  I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
                  [The QA section does it automatically now, I hope we soon get it on regular forums as well]


                  D 1 Reply Last reply
                  0
                  • L Luc Pattyn

                    that would be good, if only it got displayed on top of this forum so all can see it. :)

                    Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                    I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
                    [The QA section does it automatically now, I hope we soon get it on regular forums as well]


                    D Offline
                    D Offline
                    DaveAuld
                    wrote on last edited by
                    #9

                    Luc Pattyn wrote:

                    that would be good, if only it got displayed on top of this forum so all can see it.

                    :thumbsup: I mentioned it previously that it was maybe time to splinter of the formum for VB6, and let it die gracefully in the background, and rename this one VB.Net VB6 is the new Fight Club First rule of VB6 - You don't talk about VB6!

                    Dave Don't forget to rate messages! Find Me On: Web|Facebook|Twitter|LinkedIn Waving? dave.m.auld[at]googlewave.com

                    L 1 Reply Last reply
                    0
                    • D DaveAuld

                      Luc Pattyn wrote:

                      that would be good, if only it got displayed on top of this forum so all can see it.

                      :thumbsup: I mentioned it previously that it was maybe time to splinter of the formum for VB6, and let it die gracefully in the background, and rename this one VB.Net VB6 is the new Fight Club First rule of VB6 - You don't talk about VB6!

                      Dave Don't forget to rate messages! Find Me On: Web|Facebook|Twitter|LinkedIn Waving? dave.m.auld[at]googlewave.com

                      L Offline
                      L Offline
                      Luc Pattyn
                      wrote on last edited by
                      #10

                      the problem with splitting this forum (or any other) is it would leave a lot of old messages in the wrong half. :)

                      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                      I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
                      [The QA section does it automatically now, I hope we soon get it on regular forums as well]


                      L 1 Reply Last reply
                      0
                      • L Luc Pattyn

                        the problem with splitting this forum (or any other) is it would leave a lot of old messages in the wrong half. :)

                        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                        I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
                        [The QA section does it automatically now, I hope we soon get it on regular forums as well]


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

                        Granted, but the benefits of a split outweigh the inconvenience of having old messages either here or in the newly split VB6 forum. Since, I can only assume it is safe to say that the majority of old messages or threads revolve around .net, and a solid percentage of the posts of VB6 are a standard 'upgrade to .net, ms no longer supports vb6, or you can't do that in vb6' posts then it would be wiser to leave the content within the vb.net forum as it is more likely to be beneficial to other members here. Splitting them isn't that bad an idea.

                        Check out the CodeProject forum Guidelines[^] The original soapbox 1.0 is back![^]

                        1 Reply Last reply
                        0
                        • R RachelSo

                          My original code looked like this:

                          If strNumber <> vbNull And strProvider <> vbNull And strEmail <> vbNull Then

                          But that would throw an error @ strProvider saying that i was trying to convert a string to a double. So I changed it to:

                          If strNumber <> vbNull & strProvider <> vbNull & strEmail <> vbNull Then

                          (replaced "And" with '&') Now it throws an error @ strEmail saying that im trying to convert string strEmail to a Boolean. Can someone please tell me what Im doing wrong?

                          R Offline
                          R Offline
                          RachelSo
                          wrote on last edited by
                          #12

                          Thanks alot for the input! It works now :D

                          T 1 Reply Last reply
                          0
                          • R RachelSo

                            Thanks alot for the input! It works now :D

                            T Offline
                            T Offline
                            Thomas Krojer
                            wrote on last edited by
                            #13

                            Well, it works. But before you use the next time the "&" operator, you should know that he is the string concatenation operator (yes, also in VBx age ...)

                            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