Is there a difference between passing "" and passing Nothing to a Windows API ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • academic

    Is there a difference between passing "" and passing Nothing to a Windows API ?

    When I declare a reference variable I initialize it to Nothing.

    Now I'm wondering if that best for String variables - is "" better?

    With Nothing I assume no memory is set aside nor GC'ed

    But with "" it is - correct?

    The system seems to handle a null the same as "".

    For example:

    s=A & "more text" 'Works the same if A=Nothing or A=""

    Does this run the same code?

    What about passing to a Windows API?

    Is there a difference between passing "" and passing Nothing?


    Thank you










  • AMercer

    #2
    RE: Is there a difference between passing "" and passing Nothing to a

    > Is there a difference between passing "" and passing Nothing?

    They are different. The simplest example of the difference is with
    i = Len(s)
    i = s.Length
    s.Length will fail if s is nothing. Some vb string handling will work fine
    (and identically) with "" and nothing. Generally, vb legacy string handling
    copes with both, but object oriented functionality treats them differently.
    FYI, my habit is to avoid Nothing with strings.

    Comment

    • m.posseth

      #3
      Re: Is there a difference between passing "" and passing Nothing to a Windows API ?

      it is considered good coding practice to initialize string values with a
      empty string value

      dim x as string =""
      or
      dim x as string=string.e mpty


      if you do not do this you should always use this

      if not x is nothing then
      ' must check for nothing before processing
      end if

      regards

      Michel Posseth [MCP]



      " academic" <academic@a-znet.com> schreef in bericht
      news:eTCTLL%23L GHA.500@TK2MSFT NGP15.phx.gbl.. .[color=blue]
      > When I declare a reference variable I initialize it to Nothing.
      >
      > Now I'm wondering if that best for String variables - is "" better?
      >
      > With Nothing I assume no memory is set aside nor GC'ed
      >
      > But with "" it is - correct?
      >
      > The system seems to handle a null the same as "".
      >
      > For example:
      >
      > s=A & "more text" 'Works the same if A=Nothing or A=""
      >
      > Does this run the same code?
      >
      > What about passing to a Windows API?
      >
      > Is there a difference between passing "" and passing Nothing?
      >
      >
      > Thank you
      >
      >
      >
      >
      >
      >
      >
      >
      >
      >[/color]


      Comment

      • Mattias Sjögren

        #4
        Re: Is there a difference between passing &quot;&quot; and passing Nothing to a Windows API ?

        >But with "" it is - correct?

        Yes. But that shouldn't stop you from using it if appropriate.

        [color=blue]
        >The system seems to handle a null the same as "".[/color]

        Depends on what you mean by "the system". The VB language and
        libraries often tries to hide the difference but in the the .NET
        framework there's usually a significant difference.

        [color=blue]
        >What about passing to a Windows API?
        >
        >Is there a difference between passing "" and passing Nothing?[/color]

        Yes.


        Mattias

        --
        Mattias Sjögren [C# MVP] mattias @ mvps.org
        http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
        Please reply only to the newsgroup.

        Comment

        • Mattias Sjögren

          #5
          Re: Is there a difference between passing &quot;&quot; and passing Nothing to a Windows API ?

          [color=blue]
          >it is considered good coding practice to initialize string values with a
          >empty string value[/color]

          It is? I would only do so if I had a good reason for it, not so I
          could be lazy and skip proper null checking later on.


          Mattias

          --
          Mattias Sjögren [C# MVP] mattias @ mvps.org
          http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
          Please reply only to the newsgroup.

          Comment

          • academic

            #6
            Re: Is there a difference between passing &quot;&quot; and passing Nothing to a Windows API ?


            "m.posseth" <posseth@planet .nl> wrote in message
            news:uIV$vt$LGH A.2416@TK2MSFTN GP15.phx.gbl...[color=blue]
            > it is considered good coding practice to initialize string values with a
            > empty string value
            >
            > dim x as string =""
            > or
            > dim x as string=string.e mpty
            >
            >[/color]
            Are these equivalent

            I mean, does x end up the same?

            Thanks


            Comment

            • academic

              #7
              Re: Is there a difference between passing &quot;&quot; and passing Nothing to a


              "AMercer" <AMercer@discus sions.microsoft .com> wrote in message
              news:5C395B35-A0FB-4200-AF2E-DFE7EB44B58E@mi crosoft.com...[color=blue][color=green]
              >> Is there a difference between passing "" and passing Nothing?[/color]
              >
              > They are different. The simplest example of the difference is with
              > i = Len(s)
              > i = s.Length
              > s.Length will fail if s is nothing. Some vb string handling will work
              > fine
              > (and identically) with "" and nothing. Generally, vb legacy string
              > handling
              > copes with both, but object oriented functionality treats them
              > differently.
              > FYI, my habit is to avoid Nothing with strings.[/color]

              I going to make this my habit too. No reason not to - except with Windows
              Api calls.

              Thanks


              Comment

              • academic

                #8
                Re: Is there a difference between passing &quot;&quot; and passing Nothing to a Windows API ?


                "Mattias Sjögren" <mattias.dont.w ant.spam@mvps.o rg> wrote in message
                news:el6hSOAMGH A.3432@tk2msftn gp13.phx.gbl...[color=blue]
                >[color=green]
                >>it is considered good coding practice to initialize string values with a
                >>empty string value[/color]
                >
                > It is? I would only do so if I had a good reason for it, not so I
                > could be lazy and skip proper null checking later on.
                >
                >[/color]
                I wonder why not - is there some reason to use Nothing and then check?


                Comment

                • m.posseth

                  #9
                  Re: Is there a difference between passing &quot;&quot; and passing Nothing to a Windows API ?

                  [color=blue]
                  > It is? I would only do so if I had a good reason for it, not so I
                  > could be lazy and skip proper null checking later on.[/color]

                  According to "practical guidelines and best practices for Microsoft Visual
                  Basic and Visual C# Developers"

                  so by Francesco Balena and Giusseppe Dimauro and MS Press it is :-)

                  Why :

                  Explicit assignment avoids NullReferenceEx ception errors when referencing
                  the string and simplifies code ( because the string doesn`t have to be
                  tested against null )

                  note :

                  initializing this way points to the only zero length string that is
                  allocated in the string intern heap so it doesn`t waste memory as some
                  developers believe



                  regards

                  Michel Posseth [MCP]



                  "Mattias Sjögren" <mattias.dont.w ant.spam@mvps.o rg> schreef in bericht
                  news:el6hSOAMGH A.3432@tk2msftn gp13.phx.gbl...[color=blue]
                  >[color=green]
                  >>it is considered good coding practice to initialize string values with a
                  >>empty string value[/color]
                  >
                  > It is? I would only do so if I had a good reason for it, not so I
                  > could be lazy and skip proper null checking later on.
                  >
                  >
                  > Mattias
                  >
                  > --
                  > Mattias Sjögren [C# MVP] mattias @ mvps.org
                  > http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
                  > Please reply only to the newsgroup.[/color]


                  Comment

                  • Herfried K. Wagner [MVP]

                    #10
                    Re: Is there a difference between passing &quot;&quot; and passing Nothing to a Windows API ?

                    " academic" <academic@a-znet.com> schrieb:[color=blue][color=green]
                    >> it is considered good coding practice to initialize string values with a
                    >> empty string value
                    >>
                    >> dim x as string =""
                    >> or
                    >> dim x as string=string.e mpty[/color]
                    >
                    > Are these equivalent
                    >
                    > I mean, does x end up the same?[/color]

                    Yes, it ends up in an empty string (string of length zero) too.

                    --
                    M S Herfried K. Wagner
                    M V P <URL:http://dotnet.mvps.org/>
                    V B <URL:http://classicvb.org/petition/>

                    Comment

                    • Herfried K. Wagner [MVP]

                      #11
                      Re: Is there a difference between passing &quot;&quot; and passing Nothing to a Windows API ?

                      "m.posseth" <posseth@planet .nl> schrieb:[color=blue][color=green]
                      >> It is? I would only do so if I had a good reason for it, not so I
                      >> could be lazy and skip proper null checking later on.[/color]
                      >
                      > According to "practical guidelines and best practices for Microsoft
                      > Visual Basic and Visual C# Developers"
                      >
                      > so by Francesco Balena and Giusseppe Dimauro and MS Press it is :-)
                      >
                      > Why :
                      >
                      > Explicit assignment avoids NullReferenceEx ception errors when referencing
                      > the string and simplifies code ( because the string doesn`t have to be
                      > tested against null )[/color]

                      Mhm... That's why I use 'Right', 'Left', 'Len', etc. if I do not want
                      exceptions to be thrown on string variables referencing 'Nothing'.

                      BTW: I do not think that initializing string variables to an empty
                      (zero-length) string is good practice too.

                      --
                      M S Herfried K. Wagner
                      M V P <URL:http://dotnet.mvps.org/>
                      V B <URL:http://classicvb.org/petition/>

                      Comment

                      • academic

                        #12
                        Re: Is there a difference between passing &quot;&quot; and passing Nothing to a Windows API ?


                        "Mattias Sjögren" <mattias.dont.w ant.spam@mvps.o rg> wrote in message
                        news:eRfGGMAMGH A.3432@tk2msftn gp13.phx.gbl...[color=blue][color=green]
                        > >But with "" it is - correct?[/color]
                        >
                        > Yes. But that shouldn't stop you from using it if appropriate.[/color]
                        I can't think of a situation where it is appropriate ( which is not to imply
                        that I think there isn't any)
                        [color=blue]
                        >
                        >[color=green]
                        >>The system seems to handle a null the same as "".[/color]
                        >
                        > Depends on what you mean by "the system". The VB language and
                        > libraries often tries to hide the difference but in the the .NET
                        > framework there's usually a significant difference.[/color]

                        That's explains a lot[color=blue]
                        >
                        >[color=green]
                        >>What about passing to a Windows API?
                        >>
                        >>Is there a difference between passing "" and passing Nothing?[/color]
                        >
                        > Yes.
                        >[/color]
                        I believe passing Nothing would cause a zero to be put on the stack.

                        What does pass "" do?



                        Thanks



                        Comment

                        • m.posseth

                          #13
                          Re: Is there a difference between passing &quot;&quot; and passing Nothing to a Windows API ?

                          "" and string.empty are perfectly equivalant

                          prove ??


                          dim x as string =""
                          dim y as string = string.empty

                          string.Referenc eEquals(x,y)

                          this will display true


                          regards

                          Michel Posseth [MCP]


                          " academic" <academic@a-znet.com> schreef in bericht
                          news:u7FGUTAMGH A.676@TK2MSFTNG P10.phx.gbl...[color=blue]
                          >
                          > "m.posseth" <posseth@planet .nl> wrote in message
                          > news:uIV$vt$LGH A.2416@TK2MSFTN GP15.phx.gbl...[color=green]
                          >> it is considered good coding practice to initialize string values with a
                          >> empty string value
                          >>
                          >> dim x as string =""
                          >> or
                          >> dim x as string=string.e mpty
                          >>
                          >>[/color]
                          > Are these equivalent
                          >
                          > I mean, does x end up the same?
                          >
                          > Thanks
                          >[/color]


                          Comment

                          • m.posseth

                            #14
                            Re: Is there a difference between passing &quot;&quot; and passing Nothing to a Windows API ?

                            > Mhm... That's why I use 'Right', 'Left', 'Len', etc. if I do not want[color=blue]
                            > exceptions to be thrown on string variables referencing 'Nothing'.[/color]

                            I did exactly that for the obvious same reassons ,,,

                            however i have recently changed my coding style
                            [color=blue]
                            > BTW: I do not think that initializing string variables to an empty
                            > (zero-length) string is good practice too.[/color]

                            We live in a free country ( well ,, i know you and i do :-) ) so you
                            can always do as you please

                            however fact is that this book is written by the author of all the
                            programming microsoft visual basic core references
                            ( 6 , 2002, 2003 and the 2005 versions ) and this is a MS press book
                            ( i was also surprised i must admit but it is verry clear in it , and after
                            reading the explanation i am on there side with my coding style ) .

                            i am curious why you think it is not good coding practice


                            "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> schreef in bericht
                            news:%23ObDjZAM GHA.1424@TK2MSF TNGP12.phx.gbl. ..[color=blue]
                            > "m.posseth" <posseth@planet .nl> schrieb:[color=green][color=darkred]
                            >>> It is? I would only do so if I had a good reason for it, not so I
                            >>> could be lazy and skip proper null checking later on.[/color]
                            >>
                            >> According to "practical guidelines and best practices for Microsoft
                            >> Visual Basic and Visual C# Developers"
                            >>
                            >> so by Francesco Balena and Giusseppe Dimauro and MS Press it is :-)
                            >>
                            >> Why :
                            >>
                            >> Explicit assignment avoids NullReferenceEx ception errors when referencing
                            >> the string and simplifies code ( because the string doesn`t have to be
                            >> tested against null )[/color]
                            >
                            > Mhm... That's why I use 'Right', 'Left', 'Len', etc. if I do not want
                            > exceptions to be thrown on string variables referencing 'Nothing'.
                            >
                            > BTW: I do not think that initializing string variables to an empty
                            > (zero-length) string is good practice too.
                            >
                            > --
                            > M S Herfried K. Wagner
                            > M V P <URL:http://dotnet.mvps.org/>
                            > V B <URL:http://classicvb.org/petition/>[/color]


                            Comment

                            • academic

                              #15
                              Re: Is there a difference between passing &quot;&quot; and passing Nothing to a Windows API ?


                              Why isn't it a good practice?
                              [color=blue]
                              > BTW: I do not think that initializing string variables to an empty
                              > (zero-length) string is good practice too.
                              >
                              > --
                              > M S Herfried K. Wagner
                              > M V P <URL:http://dotnet.mvps.org/>
                              > V B <URL:http://classicvb.org/petition/>[/color]


                              Comment

                              Working...