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

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

    great thanks
    "m.posseth" <posseth@planet .nl> wrote in message
    news:%23JL0VaAM GHA.3432@tk2msf tngp13.phx.gbl. ..[color=blue]
    > "" and string.empty are perfectly equivalent
    >
    > 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=green]
    >>
    >> "m.posseth" <posseth@planet .nl> wrote in message
    >> news:uIV$vt$LGH A.2416@TK2MSFTN GP15.phx.gbl...[color=darkred]
    >>> 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]
    >
    >[/color]


    Comment

    • academic

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

      Thanks
      "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
      news:eml97ZAMGH A.3984@TK2MSFTN GP14.phx.gbl...[color=blue]
      >" academic" <academic@a-znet.com> schrieb:[color=green][color=darkred]
      >>> 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/>[/color]


      Comment

      • CMM

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

        Unless you use a Nothing-String to denote some "meaning" other than Empty,
        there is no reason to NOT initialize it to Empty. If you really WANT
        *Nothing* for some reason, then that's a different story. But, if you are
        just relying on VB's comparison operators and intrinsic functions
        (Left,Right,Len , etc) to keep you safe and interpret that Nothing-String as
        Empty, you are for sure treading on shaky ground... and it is for sure a BAD
        coding practice.

        Laziness... not initializing a string and relying on VB's hand-holding is
        the same as using Evil-Type-Coercion that we've all gotten past years ago.
        Suppose after you leave your company a developer comes along and wants to
        add a feature that uses one of the classes that you've written....
        If HerfriedWagnerA wesomeControl.T itle.Length < 10 Then....
        doh! EXCEPTION!
        Sadly, this error might not manifest itself until N point in the future when
        the app is in Production.


        --
        -C. Moya

        "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
        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

        • Herfried K. Wagner [MVP]

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

          " academic" <academic@a-znet.com> schrieb:[color=blue]
          > Why isn't it a good practice?
          >[color=green]
          >> BTW: I do not think that initializing string variables to an empty
          >> (zero-length) string is good practice too.[/color][/color]

          I believe it strongly depends on the situation. IMO it's not a good idea to
          propose "initialize strings with an empty string" as a general rule.
          However, it still may make sense in some cases.

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

          Comment

          • CMM

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

            The performance differences aren't really noticeable but there IS a
            difference!

            In the IL:
            ("") compiles to ldstr "" --- Which means, LoadString, Look it up ("it"
            being the char array) in the string table, create a new reference to the
            existing string.
            (String.Empty) compiles to ldsfld String::Empty --- Which means, LoadField,
            push an already existing reference.

            Look at it this way:
            At its core, S = "" is the same thing as S = New String(New Char() {})
            While S = String.Empty is just quite literally S = String.Empty.

            If you have "" executed 50 times in your running code, you have created (and
            instantly discarded) 50 object references. Sure, all the objects point to
            the same string in the string table but still!

            2) Lastly, "" forces a scan of the string table (obviously) while
            String.Empty does not.


            --
            -C. Moya



            Comment

            • academic

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

              Sounds like the last word to me


              Thanks

              "CMM" <cmm@nospam.com > wrote in message
              news:uC1rLjCMGH A.3264@TK2MSFTN GP11.phx.gbl...[color=blue]
              > The performance differences aren't really noticeable but there IS a
              > difference!
              >
              > In the IL:
              > ("") compiles to ldstr "" --- Which means, LoadString, Look it up ("it"
              > being the char array) in the string table, create a new reference to the
              > existing string.
              > (String.Empty) compiles to ldsfld String::Empty --- Which means,
              > LoadField, push an already existing reference.
              >
              > Look at it this way:
              > At its core, S = "" is the same thing as S = New String(New Char() {})
              > While S = String.Empty is just quite literally S = String.Empty.
              >
              > If you have "" executed 50 times in your running code, you have created
              > (and instantly discarded) 50 object references. Sure, all the objects
              > point to the same string in the string table but still!
              >
              > 2) Lastly, "" forces a scan of the string table (obviously) while
              > String.Empty does not.
              >
              >
              > --
              > -C. Moya
              > www.cmoya.com
              >[/color]


              Comment

              • CMM

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

                "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
                news:ua9IMPCMGH A.3460@TK2MSFTN GP15.phx.gbl...[color=blue]
                >" academic" <academic@a-znet.com> schrieb:
                > I believe it strongly depends on the situation. IMO it's not a good idea
                > to propose "initialize strings with an empty string" as a general rule.
                > However, it still may make sense in some cases.[/color]

                What is the benefit of an uninitialized string? Why is it not a "good idea?"
                I mean...
                Questions:
                1) Are there situations where <Nothing> actually *means* something in your
                code and your algorithms other than <Empty>?
                2) Or are you just relying on VB's intrinsic functions (Len, Left, Mid, etc)
                and "coercion" to safely deal with the string? If so, how do you justify
                that?... isn't that the same as using Option-"Loose" "Evil-Type-Coercion?"
                3) Is it for performance or memory reasons?

                --
                -C. Moya



                Comment

                • CMM

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

                  " academic" <academic@a-znet.com> wrote in message ...[color=blue]
                  > Sounds like the last word to me[/color]

                  To clarify... because your original post brought up slightly different
                  questions:

                  1) Nothing = Nothing. You're right in assuming that nothing is allocated. A
                  string initializes to Nothing by default. This leads to bugs... if not for
                  you then for someone down the road who might have to maintain your code!
                  While VB's type coercion often hides this from you... for instance trying
                  s.Length() on a Nothing string will result in an exception. VB won't help
                  you there.

                  2) "" = New String(New Char() {})

                  3) String.Empty = .... um, well, it equals exactly "" ;-)

                  Obviously, Dim s As String = String.Empty is by far the most efficient and
                  safest coding practice. I recommend it.

                  --
                  -C. Moya

                  " academic" <academic@a-znet.com> wrote in message
                  news:ufSKEqCMGH A.2276@TK2MSFTN GP15.phx.gbl...[color=blue]
                  > Sounds like the last word to me
                  >
                  >
                  > Thanks
                  >
                  > "CMM" <cmm@nospam.com > wrote in message
                  > news:uC1rLjCMGH A.3264@TK2MSFTN GP11.phx.gbl...[color=green]
                  >> The performance differences aren't really noticeable but there IS a
                  >> difference!
                  >>
                  >> In the IL:
                  >> ("") compiles to ldstr "" --- Which means, LoadString, Look it up ("it"
                  >> being the char array) in the string table, create a new reference to the
                  >> existing string.
                  >> (String.Empty) compiles to ldsfld String::Empty --- Which means,
                  >> LoadField, push an already existing reference.
                  >>
                  >> Look at it this way:
                  >> At its core, S = "" is the same thing as S = New String(New Char() {})
                  >> While S = String.Empty is just quite literally S = String.Empty.
                  >>
                  >> If you have "" executed 50 times in your running code, you have created
                  >> (and instantly discarded) 50 object references. Sure, all the objects
                  >> point to the same string in the string table but still!
                  >>
                  >> 2) Lastly, "" forces a scan of the string table (obviously) while
                  >> String.Empty does not.
                  >>
                  >>
                  >> --
                  >> -C. Moya
                  >> www.cmoya.com
                  >>[/color]
                  >
                  >[/color]


                  Comment

                  • Herfried K. Wagner [MVP]

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

                    "CMM" <cmm@nospam.com > schrieb:[color=blue][color=green]
                    >> I believe it strongly depends on the situation. IMO it's not a good idea
                    >> to propose "initialize strings with an empty string" as a general rule.
                    >> However, it still may make sense in some cases.[/color]
                    >
                    > What is the benefit of an uninitialized string? Why is it not a "good
                    > idea?" I mean...
                    > Questions:
                    > 1) Are there situations where <Nothing> actually *means* something in
                    > your code and your algorithms other than <Empty>?[/color]

                    Yes! The whole discussion is similar to the 'NULL' vs. zero-length string
                    or 'NULL' vs. 0 discussion for databases. A zero-length string is different
                    from a 'Nothing' reference. Inside the .NET Framework's class library they
                    are not treated as equal.
                    [color=blue]
                    > 2) Or are you just relying on VB's intrinsic functions (Len, Left, Mid,
                    > etc) and "coercion" to safely deal with the string?[/color]

                    VB's functions mostly treat "" and 'Nothing' string references as equal.
                    This means that 'Len', for example, returns 0 for both a 'Nothing' reference
                    and a zero-length string. In case of a method returning either a 'Nothing'
                    reference or a string of arbitrary length I'd choose 'If Len(s) > 0 Then'
                    over 'If s IsNot Nothing AndAlso s.Length > 0 Then' to check if a string
                    object with length greater than 0 has been returned.
                    [color=blue]
                    > 3) Is it for performance or memory reasons?[/color]

                    Performance and memory reasons are not important for me in most cases. I
                    prefer to write semantically clear code instead of performing
                    micro-optimizations.

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

                    Comment

                    • CMM

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

                      > Inside the .NET Framework's class library they are not treated as equal.

                      So why do *you* treat them as if they were?

                      I mean in *your* code and in your *your algorithms* do you explicitely use
                      Nothing to accomplish something or do you just let VB's intrinsic operators
                      or functions interpret it as Empty? In other words, is Nothing treated as
                      Empty everywhere in your code? To me, that's the same exact thing as writing
                      stuff like S = 5 where S is a string... when Option Explicit is Off. That's
                      not "good practice."

                      You really have to justify why you say "...it's not a good idea to
                      'initialize strings with an empty string' as a general rule." Why isn't it a
                      good idea? I mean, 99% of the time, I expect my strings to be Empty not
                      Nothing. Why not declare them as Empty rather than have VB *coerce* the
                      value??? That's what I'm trying to understand. I'm not saying you're
                      wrong... I'm just saying prima facie it doesn't seem to make sense.


                      --
                      -C. Moya



                      Comment

                      • Cor Ligthert [MVP]

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

                        Herfried,

                        I did not read the whole message just curious where CMM and you where
                        disussing about.

                        However I would never compare a database with computer memory.

                        In a database means a Null field, really nothing which means than as well no
                        disk space.

                        In memory that space will be used for other data in another situation of the
                        method, so there will not be any advantage using it or not. (The memory is
                        not perstistent).

                        Just my thought,

                        Cor



                        Comment

                        • Armin Zingler

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

                          "m.posseth" <posseth@planet .nl> schrieb[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]


                          I've never done it.


                          Armin

                          Comment

                          • Armin Zingler

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

                            " academic" <academic@a-znet.com> schrieb[color=blue]
                            >
                            > Why isn't it a good practice?[/color]


                            If it has to be an empty string always, use a constant.


                            Armin

                            Comment

                            • m.posseth

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


                              well i am confused now


                              Francesco and Giuseppe are verry clear in there book, that both are even
                              good

                              "" and string.empty

                              however from a developers perspective i would use "" as it is faster to
                              write as string.empty
                              untill i heard from CMM that the IL produces different code for
                              string.empty or ""

                              strange .....

                              Michel





                              "Armin Zingler" <az.nospam@free net.de> schreef in bericht
                              news:OcwEuLHMGH A.2580@TK2MSFTN GP14.phx.gbl...[color=blue]
                              >" academic" <academic@a-znet.com> schrieb[color=green]
                              >>
                              >> Why isn't it a good practice?[/color]
                              >
                              >
                              > If it has to be an empty string always, use a constant.
                              >
                              >
                              > Armin[/color]


                              Comment

                              • Cor Ligthert [MVP]

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

                                Academic,

                                A "" pases a string with one charachter ""

                                A Nothing passes an object with no reference.

                                You cam try this
                                \\\
                                Dim myfirstobject As Object = Nothing
                                Dim mysecondobject As String
                                If myfirstobject Is mysecondobject Then
                                MessageBox.Show ("we both are Nothing")
                                End If
                                If myfirstobject IsNot "" Then
                                MessageBox.Show ("we are different")
                                End If
                                If myfirstobject IsNot String.Empty Then
                                MessageBox.Show ("we are different")
                                End If
                                ///

                                I hope this gives some idea's

                                Cor


                                Comment

                                Working...