Empty string comparisons

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

    #1

    Empty string comparisons

    Hi all,

    I am having a memory blank at the moment. I have been writing in C# for a
    number of years and now need to do something in VB.NET, so forgive me such a
    primitive question.

    In C#, I test whether a string has a value or not by the following syntax:

    if (thisString.Tri m() == "")
    {
    // if true
    something()
    }
    else
    {
    // if false
    somethingelse()
    }

    Now, in VB.NET an equivalent does not seem to work:

    If thisString.Trim () = "" Then
    ' if true
    something()
    Else
    ' if false
    somethingelse()
    End If

    I cannot get past a False return for the VB comparison. What is the best
    method for testing whether a string is empty or null?


    Regards,
    Neville Lang


  • Pritcham

    #2
    Re: Empty string comparisons

    Hi Neville

    Don't know whether this is 'best' or not but it certainly works:

    If testStr Is Nothing or Len(Trim(testSt r)) = 0 Then
    ' 0 length or null string
    Else
    ' string is initialised and not empty
    End if

    Cheers
    Martin

    Neville Lang wrote:
    Hi all,
    >
    I am having a memory blank at the moment. I have been writing in C# for a
    number of years and now need to do something in VB.NET, so forgive me such a
    primitive question.
    >
    In C#, I test whether a string has a value or not by the following syntax:
    >
    if (thisString.Tri m() == "")
    {
    // if true
    something()
    }
    else
    {
    // if false
    somethingelse()
    }
    >
    Now, in VB.NET an equivalent does not seem to work:
    >
    If thisString.Trim () = "" Then
    ' if true
    something()
    Else
    ' if false
    somethingelse()
    End If
    >
    I cannot get past a False return for the VB comparison. What is the best
    method for testing whether a string is empty or null?
    >
    >
    Regards,
    Neville Lang

    Comment

    • tommaso.gastaldi@uniroma1.it

      #3
      Re: Empty string comparisons


      Neville Lang ha scritto:

      Now, in VB.NET an equivalent does not seem to work:
      >
      If thisString.Trim () = "" Then
      ' if true
      something()
      Else
      ' if false
      somethingelse()
      End If

      I usually use:

      Dim s As String = ""
      If s.Trim = String.Empty Then
      MsgBox("is empty")
      Else
      MsgBox("is not empty")
      End If

      but your version also looks fine to me (unless I am missing to see
      something)...


      Tommaso
      >
      I cannot get past a False return for the VB comparison. What is the best
      method for testing whether a string is empty or null?
      >
      >
      Regards,
      Neville Lang

      Comment

      • David Anton

        #4
        RE: Empty string comparisons

        Your problem probably lies elsewhere - the VB code you posted is the exact
        equivalent to the C# code.
        --
        David Anton
        Source code converters: Convert between C#, C++, Java, and VB with the most accurate and reliable source code converters

        Instant C#: VB to C# converter
        Instant VB: C# to VB converter
        Instant C++: C#/VB to C++ converter
        Instant Python: VB to Python converter


        "Neville Lang" wrote:
        Hi all,
        >
        I am having a memory blank at the moment. I have been writing in C# for a
        number of years and now need to do something in VB.NET, so forgive me such a
        primitive question.
        >
        In C#, I test whether a string has a value or not by the following syntax:
        >
        if (thisString.Tri m() == "")
        {
        // if true
        something()
        }
        else
        {
        // if false
        somethingelse()
        }
        >
        Now, in VB.NET an equivalent does not seem to work:
        >
        If thisString.Trim () = "" Then
        ' if true
        something()
        Else
        ' if false
        somethingelse()
        End If
        >
        I cannot get past a False return for the VB comparison. What is the best
        method for testing whether a string is empty or null?
        >
        >
        Regards,
        Neville Lang
        >
        >
        >

        Comment

        • Oenone

          #5
          Re: Empty string comparisons

          Pritcham wrote:
          Hi Neville
          >
          Don't know whether this is 'best' or not but it certainly works:
          >
          If testStr Is Nothing or Len(Trim(testSt r)) = 0 Then
          ' 0 length or null string
          Else
          ' string is initialised and not empty
          End if
          Just in case you're not aware, the Len() and Trim() functions understand
          strings that are Nothing and treat them as empty strings (unlike
          testStr.Length( ) or testStr.Trim(), which would obviously throw a
          NullReferenceEx ception), so you can actually shorten this to:

          \\\
          If Len(Trim(testSt r)) = 0 Then
          ' 0 length or null string
          Else
          ' string is initialised and not empty
          End if
          ///

          --

          (O)enone


          Comment

          • Claes Bergefall

            #6
            Re: Empty string comparisons

            The code you have should work just fine. There are other ways to do it as
            well though:

            1. Use the VB.NET "feature" that null and empty string are considered equal
            when using the = operator, i.e the following statement:
            If thisString = "" Then
            returns true if thisString is either empty *or* null (beware, this does not
            work in C#)
            2. Use the new String.IsNullOr Empty method (requires .NET 2.0)

            /claes

            "Neville Lang" <neville@MAPS_O Nnjlsoftware.co mwrote in message
            news:ucODy9$9GH A.3348@TK2MSFTN GP03.phx.gbl...
            Hi all,
            >
            I am having a memory blank at the moment. I have been writing in C# for a
            number of years and now need to do something in VB.NET, so forgive me such
            a primitive question.
            >
            In C#, I test whether a string has a value or not by the following syntax:
            >
            if (thisString.Tri m() == "")
            {
            // if true
            something()
            }
            else
            {
            // if false
            somethingelse()
            }
            >
            Now, in VB.NET an equivalent does not seem to work:
            >
            If thisString.Trim () = "" Then
            ' if true
            something()
            Else
            ' if false
            somethingelse()
            End If
            >
            I cannot get past a False return for the VB comparison. What is the best
            method for testing whether a string is empty or null?
            >
            >
            Regards,
            Neville Lang
            >
            >

            Comment

            • David Anton

              #7
              Re: Empty string comparisons

              Regarding your first point, VB is even stranger than that:
              A string set to Nothng (or uninitialized) is regarded as equal to an empty
              string, but an empty string is *not* regarded as equal to Nothing. C# is
              consistent in this respect: a null string is a null string and an empty
              string is an empty string.
              --
              David Anton
              Source code converters: Convert between C#, C++, Java, and VB with the most accurate and reliable source code converters

              Instant C#: VB to C# converter
              Instant VB: C# to VB converter
              Instant C++: C#/VB to C++ converter
              Instant Python: VB to Python converter


              "Claes Bergefall" wrote:
              The code you have should work just fine. There are other ways to do it as
              well though:
              >
              1. Use the VB.NET "feature" that null and empty string are considered equal
              when using the = operator, i.e the following statement:
              If thisString = "" Then
              returns true if thisString is either empty *or* null (beware, this does not
              work in C#)
              2. Use the new String.IsNullOr Empty method (requires .NET 2.0)
              >
              /claes
              >
              "Neville Lang" <neville@MAPS_O Nnjlsoftware.co mwrote in message
              news:ucODy9$9GH A.3348@TK2MSFTN GP03.phx.gbl...
              Hi all,

              I am having a memory blank at the moment. I have been writing in C# for a
              number of years and now need to do something in VB.NET, so forgive me such
              a primitive question.

              In C#, I test whether a string has a value or not by the following syntax:

              if (thisString.Tri m() == "")
              {
              // if true
              something()
              }
              else
              {
              // if false
              somethingelse()
              }

              Now, in VB.NET an equivalent does not seem to work:

              If thisString.Trim () = "" Then
              ' if true
              something()
              Else
              ' if false
              somethingelse()
              End If

              I cannot get past a False return for the VB comparison. What is the best
              method for testing whether a string is empty or null?


              Regards,
              Neville Lang
              >
              >
              >

              Comment

              • Claes Bergefall

                #8
                Re: Empty string comparisons

                Actually it does (at least in 2.0). All of the following evaluate to True:

                Dim s1 As String = ""
                Dim s2 As String = Nothing
                Dim b1 As Boolean = (s1 = Nothing)
                Dim b2 As Boolean = (s2 = Nothing)
                Dim b3 As Boolean = (s1 = "")
                Dim b4 As Boolean = (s2 = "")

                You have to use String.Equals to distinguish between Nothing and an empty
                string. Personally I find the VB.NET behaviour annoying and would prefer
                that it worked the same as in C#

                /claes

                "David Anton" <DavidAnton@dis cussions.micros oft.comwrote in message
                news:63186DF2-8DE7-4EA4-9927-D6A755AD4596@mi crosoft.com...
                Regarding your first point, VB is even stranger than that:
                A string set to Nothng (or uninitialized) is regarded as equal to an empty
                string, but an empty string is *not* regarded as equal to Nothing. C# is
                consistent in this respect: a null string is a null string and an empty
                string is an empty string.
                --
                David Anton
                Source code converters: Convert between C#, C++, Java, and VB with the most accurate and reliable source code converters

                Instant C#: VB to C# converter
                Instant VB: C# to VB converter
                Instant C++: C#/VB to C++ converter
                Instant Python: VB to Python converter
                >
                >
                "Claes Bergefall" wrote:
                >
                >The code you have should work just fine. There are other ways to do it as
                >well though:
                >>
                >1. Use the VB.NET "feature" that null and empty string are considered
                >equal
                >when using the = operator, i.e the following statement:
                >If thisString = "" Then
                >returns true if thisString is either empty *or* null (beware, this does
                >not
                >work in C#)
                >2. Use the new String.IsNullOr Empty method (requires .NET 2.0)
                >>
                > /claes
                >>
                >"Neville Lang" <neville@MAPS_O Nnjlsoftware.co mwrote in message
                >news:ucODy9$9G HA.3348@TK2MSFT NGP03.phx.gbl.. .
                Hi all,
                >
                I am having a memory blank at the moment. I have been writing in C# for
                a
                number of years and now need to do something in VB.NET, so forgive me
                such
                a primitive question.
                >
                In C#, I test whether a string has a value or not by the following
                syntax:
                >
                if (thisString.Tri m() == "")
                {
                // if true
                something()
                }
                else
                {
                // if false
                somethingelse()
                }
                >
                Now, in VB.NET an equivalent does not seem to work:
                >
                If thisString.Trim () = "" Then
                ' if true
                something()
                Else
                ' if false
                somethingelse()
                End If
                >
                I cannot get past a False return for the VB comparison. What is the
                best
                method for testing whether a string is empty or null?
                >
                >
                Regards,
                Neville Lang
                >
                >
                >>
                >>
                >>

                Comment

                • Jay B. Harlow

                  #9
                  Re: Empty string comparisons

                  Claes,
                  You have to use String.Equals to distinguish between Nothing and an empty
                  string.
                  Close! ;-)

                  I would recommend using "Is" to distinguish between Nothing and an empty
                  string.
                  Dim b5 As Boolean = (s1 Is Nothing)
                  Dim b6 As Boolean = (s2 Is Nothing)
                  One might consider these two, however they won't always return true:
                  Dim b7 As Boolean = (s1 Is "")
                  Dim b8 As Boolean = (s2 Is "")
                  Depending on how s1 & s2 got to be an empty string will change what the last
                  two return.

                  For example:

                  s2 = " ".Trim()

                  Will cause (s2 Is "") to fail, as " ".Trim() returns a new string,
                  where as "" returns an interned string...

                  Remember that = does value equality ("" & Nothing) are considered to have
                  the same value.

                  Where as Is does reference equality ("", " ".Trim() & Nothing) are
                  distinct object references.
                  Personally I find the VB.NET behavior annoying and would prefer that it
                  worked the same as in C#
                  Most of the time I agree, there are times though where I prefer the C#
                  behavior.

                  --
                  Hope this helps
                  Jay B. Harlow
                  ..NET Application Architect, Enthusiast, & Evangelist
                  T.S. Bradley - http://www.tsbradley.net


                  "Claes Bergefall" <louplou@nospam .nospamwrote in message
                  news:%23f6zIUQ% 23GHA.3644@TK2M SFTNGP02.phx.gb l...
                  Actually it does (at least in 2.0). All of the following evaluate to True:
                  >
                  Dim s1 As String = ""
                  Dim s2 As String = Nothing
                  Dim b1 As Boolean = (s1 = Nothing)
                  Dim b2 As Boolean = (s2 = Nothing)
                  Dim b3 As Boolean = (s1 = "")
                  Dim b4 As Boolean = (s2 = "")
                  >
                  You have to use String.Equals to distinguish between Nothing and an empty
                  string. Personally I find the VB.NET behaviour annoying and would prefer
                  that it worked the same as in C#
                  >
                  /claes
                  >
                  "David Anton" <DavidAnton@dis cussions.micros oft.comwrote in message
                  news:63186DF2-8DE7-4EA4-9927-D6A755AD4596@mi crosoft.com...
                  >Regarding your first point, VB is even stranger than that:
                  >A string set to Nothng (or uninitialized) is regarded as equal to an
                  >empty
                  >string, but an empty string is *not* regarded as equal to Nothing. C# is
                  >consistent in this respect: a null string is a null string and an empty
                  >string is an empty string.
                  >--
                  >David Anton
                  >www.tangiblesoftwaresolutions.com
                  >Instant C#: VB to C# converter
                  >Instant VB: C# to VB converter
                  >Instant C++: C#/VB to C++ converter
                  >Instant Python: VB to Python converter
                  >>
                  >>
                  >"Claes Bergefall" wrote:
                  >>
                  >>The code you have should work just fine. There are other ways to do it
                  >>as
                  >>well though:
                  >>>
                  >>1. Use the VB.NET "feature" that null and empty string are considered
                  >>equal
                  >>when using the = operator, i.e the following statement:
                  >>If thisString = "" Then
                  >>returns true if thisString is either empty *or* null (beware, this does
                  >>not
                  >>work in C#)
                  >>2. Use the new String.IsNullOr Empty method (requires .NET 2.0)
                  >>>
                  >> /claes
                  >>>
                  >>"Neville Lang" <neville@MAPS_O Nnjlsoftware.co mwrote in message
                  >>news:ucODy9$9 GHA.3348@TK2MSF TNGP03.phx.gbl. ..
                  >Hi all,
                  >>
                  >I am having a memory blank at the moment. I have been writing in C#
                  >for a
                  >number of years and now need to do something in VB.NET, so forgive me
                  >such
                  >a primitive question.
                  >>
                  >In C#, I test whether a string has a value or not by the following
                  >syntax:
                  >>
                  >if (thisString.Tri m() == "")
                  >{
                  > // if true
                  > something()
                  >}
                  >else
                  >{
                  > // if false
                  > somethingelse()
                  >}
                  >>
                  >Now, in VB.NET an equivalent does not seem to work:
                  >>
                  >If thisString.Trim () = "" Then
                  > ' if true
                  > something()
                  >Else
                  > ' if false
                  > somethingelse()
                  >End If
                  >>
                  >I cannot get past a False return for the VB comparison. What is the
                  >best
                  >method for testing whether a string is empty or null?
                  >>
                  >>
                  >Regards,
                  >Neville Lang
                  >>
                  >>
                  >>>
                  >>>
                  >>>
                  >
                  >

                  Comment

                  • David Anton

                    #10
                    Re: Empty string comparisons

                    Yes, but try:
                    Dim s1 As String = ""
                    Dim b1 As Boolean = (s1 Is Nothing)

                    b1 evaluates to false. Isn't VB fun?!
                    --
                    David Anton
                    Source code converters: Convert between C#, C++, Java, and VB with the most accurate and reliable source code converters

                    Instant C#: VB to C# converter
                    Instant VB: C# to VB converter
                    Instant C++: C#/VB to C++ converter
                    Instant Python: VB to Python converter


                    "Claes Bergefall" wrote:
                    Actually it does (at least in 2.0). All of the following evaluate to True:
                    >
                    Dim s1 As String = ""
                    Dim s2 As String = Nothing
                    Dim b1 As Boolean = (s1 = Nothing)
                    Dim b2 As Boolean = (s2 = Nothing)
                    Dim b3 As Boolean = (s1 = "")
                    Dim b4 As Boolean = (s2 = "")
                    >
                    You have to use String.Equals to distinguish between Nothing and an empty
                    string. Personally I find the VB.NET behaviour annoying and would prefer
                    that it worked the same as in C#
                    >
                    /claes
                    >
                    "David Anton" <DavidAnton@dis cussions.micros oft.comwrote in message
                    news:63186DF2-8DE7-4EA4-9927-D6A755AD4596@mi crosoft.com...
                    Regarding your first point, VB is even stranger than that:
                    A string set to Nothng (or uninitialized) is regarded as equal to an empty
                    string, but an empty string is *not* regarded as equal to Nothing. C# is
                    consistent in this respect: a null string is a null string and an empty
                    string is an empty string.
                    --
                    David Anton
                    Source code converters: Convert between C#, C++, Java, and VB with the most accurate and reliable source code converters

                    Instant C#: VB to C# converter
                    Instant VB: C# to VB converter
                    Instant C++: C#/VB to C++ converter
                    Instant Python: VB to Python converter


                    "Claes Bergefall" wrote:
                    The code you have should work just fine. There are other ways to do it as
                    well though:
                    >
                    1. Use the VB.NET "feature" that null and empty string are considered
                    equal
                    when using the = operator, i.e the following statement:
                    If thisString = "" Then
                    returns true if thisString is either empty *or* null (beware, this does
                    not
                    work in C#)
                    2. Use the new String.IsNullOr Empty method (requires .NET 2.0)
                    >
                    /claes
                    >
                    "Neville Lang" <neville@MAPS_O Nnjlsoftware.co mwrote in message
                    news:ucODy9$9GH A.3348@TK2MSFTN GP03.phx.gbl...
                    Hi all,

                    I am having a memory blank at the moment. I have been writing in C# for
                    a
                    number of years and now need to do something in VB.NET, so forgive me
                    such
                    a primitive question.

                    In C#, I test whether a string has a value or not by the following
                    syntax:

                    if (thisString.Tri m() == "")
                    {
                    // if true
                    something()
                    }
                    else
                    {
                    // if false
                    somethingelse()
                    }

                    Now, in VB.NET an equivalent does not seem to work:

                    If thisString.Trim () = "" Then
                    ' if true
                    something()
                    Else
                    ' if false
                    somethingelse()
                    End If

                    I cannot get past a False return for the VB comparison. What is the
                    best
                    method for testing whether a string is empty or null?


                    Regards,
                    Neville Lang


                    >
                    >
                    >
                    >
                    >
                    >

                    Comment

                    • Jay B. Harlow

                      #11
                      Re: Empty string comparisons

                      >Personally I find the VB.NET behavior annoying and would prefer that it
                      >worked the same as in C#
                      Most of the time I agree, there are times though where I prefer the C#
                      behavior.
                      Huh? (cringle nose).

                      That didn't come out right. :-) I meant most of the item I agree; Most of
                      the time I prefer the C# behavior. However there are times I like the VB
                      behavior...

                      --
                      Hope this helps
                      Jay B. Harlow
                      ..NET Application Architect, Enthusiast, & Evangelist
                      T.S. Bradley - http://www.tsbradley.net


                      "Jay B. Harlow" <Jay_Harlow_MVP @tsbradley.netw rote in message
                      news:27A229FC-862A-47A5-8527-A2FDAD88773B@mi crosoft.com...
                      Claes,
                      >You have to use String.Equals to distinguish between Nothing and an empty
                      >string.
                      Close! ;-)
                      >
                      I would recommend using "Is" to distinguish between Nothing and an empty
                      string.
                      >
                      >Dim b5 As Boolean = (s1 Is Nothing)
                      >Dim b6 As Boolean = (s2 Is Nothing)
                      >
                      One might consider these two, however they won't always return true:
                      >
                      >Dim b7 As Boolean = (s1 Is "")
                      >Dim b8 As Boolean = (s2 Is "")
                      >
                      Depending on how s1 & s2 got to be an empty string will change what the
                      last two return.
                      >
                      For example:
                      >
                      s2 = " ".Trim()
                      >
                      Will cause (s2 Is "") to fail, as " ".Trim() returns a new string,
                      where as "" returns an interned string...
                      >
                      Remember that = does value equality ("" & Nothing) are considered to have
                      the same value.
                      >
                      Where as Is does reference equality ("", " ".Trim() & Nothing) are
                      distinct object references.
                      >
                      >Personally I find the VB.NET behavior annoying and would prefer that it
                      >worked the same as in C#
                      Most of the time I agree, there are times though where I prefer the C#
                      behavior.
                      >
                      --
                      Hope this helps
                      Jay B. Harlow
                      .NET Application Architect, Enthusiast, & Evangelist
                      T.S. Bradley - http://www.tsbradley.net
                      >
                      >
                      "Claes Bergefall" <louplou@nospam .nospamwrote in message
                      news:%23f6zIUQ% 23GHA.3644@TK2M SFTNGP02.phx.gb l...
                      >Actually it does (at least in 2.0). All of the following evaluate to
                      >True:
                      >>
                      >Dim s1 As String = ""
                      >Dim s2 As String = Nothing
                      >Dim b1 As Boolean = (s1 = Nothing)
                      >Dim b2 As Boolean = (s2 = Nothing)
                      >Dim b3 As Boolean = (s1 = "")
                      >Dim b4 As Boolean = (s2 = "")
                      >>
                      >You have to use String.Equals to distinguish between Nothing and an empty
                      >string. Personally I find the VB.NET behaviour annoying and would prefer
                      >that it worked the same as in C#
                      >>
                      > /claes
                      >>
                      >"David Anton" <DavidAnton@dis cussions.micros oft.comwrote in message
                      >news:63186DF 2-8DE7-4EA4-9927-D6A755AD4596@mi crosoft.com...
                      >>Regarding your first point, VB is even stranger than that:
                      >>A string set to Nothng (or uninitialized) is regarded as equal to an
                      >>empty
                      >>string, but an empty string is *not* regarded as equal to Nothing. C#
                      >>is
                      >>consistent in this respect: a null string is a null string and an empty
                      >>string is an empty string.
                      >>--
                      >>David Anton
                      >>www.tangiblesoftwaresolutions.com
                      >>Instant C#: VB to C# converter
                      >>Instant VB: C# to VB converter
                      >>Instant C++: C#/VB to C++ converter
                      >>Instant Python: VB to Python converter
                      >>>
                      >>>
                      >>"Claes Bergefall" wrote:
                      >>>
                      >>>The code you have should work just fine. There are other ways to do it
                      >>>as
                      >>>well though:
                      >>>>
                      >>>1. Use the VB.NET "feature" that null and empty string are considered
                      >>>equal
                      >>>when using the = operator, i.e the following statement:
                      >>>If thisString = "" Then
                      >>>returns true if thisString is either empty *or* null (beware, this does
                      >>>not
                      >>>work in C#)
                      >>>2. Use the new String.IsNullOr Empty method (requires .NET 2.0)
                      >>>>
                      >>> /claes
                      >>>>
                      >>>"Neville Lang" <neville@MAPS_O Nnjlsoftware.co mwrote in message
                      >>>news:ucODy9$ 9GHA.3348@TK2MS FTNGP03.phx.gbl ...
                      >>Hi all,
                      >>>
                      >>I am having a memory blank at the moment. I have been writing in C#
                      >>for a
                      >>number of years and now need to do something in VB.NET, so forgive me
                      >>such
                      >>a primitive question.
                      >>>
                      >>In C#, I test whether a string has a value or not by the following
                      >>syntax:
                      >>>
                      >>if (thisString.Tri m() == "")
                      >>{
                      >> // if true
                      >> something()
                      >>}
                      >>else
                      >>{
                      >> // if false
                      >> somethingelse()
                      >>}
                      >>>
                      >>Now, in VB.NET an equivalent does not seem to work:
                      >>>
                      >>If thisString.Trim () = "" Then
                      >> ' if true
                      >> something()
                      >>Else
                      >> ' if false
                      >> somethingelse()
                      >>End If
                      >>>
                      >>I cannot get past a False return for the VB comparison. What is the
                      >>best
                      >>method for testing whether a string is empty or null?
                      >>>
                      >>>
                      >>Regards,
                      >>Neville Lang
                      >>>
                      >>>
                      >>>>
                      >>>>
                      >>>>
                      >>
                      >>
                      >

                      Comment

                      • Neville Lang

                        #12
                        Re: Empty string comparisons

                        HI all,

                        Thank you for your replies, they were helpful. From what I thought was a
                        fairly basic question, the responses did yield some interesting things in VB
                        v C#.

                        Regards,
                        Neville Lang



                        "Neville Lang" <neville@MAPS_O Nnjlsoftware.co mwrote in message
                        news:ucODy9$9GH A.3348@TK2MSFTN GP03.phx.gbl...
                        Hi all,
                        >
                        I am having a memory blank at the moment. I have been writing in C# for a
                        number of years and now need to do something in VB.NET, so forgive me such
                        a primitive question.
                        >
                        In C#, I test whether a string has a value or not by the following syntax:
                        >
                        if (thisString.Tri m() == "")
                        {
                        // if true
                        something()
                        }
                        else
                        {
                        // if false
                        somethingelse()
                        }
                        >
                        Now, in VB.NET an equivalent does not seem to work:
                        >
                        If thisString.Trim () = "" Then
                        ' if true
                        something()
                        Else
                        ' if false
                        somethingelse()
                        End If
                        >
                        I cannot get past a False return for the VB comparison. What is the best
                        method for testing whether a string is empty or null?
                        >
                        >
                        Regards,
                        Neville Lang
                        >
                        >

                        Comment

                        • Neville Lang

                          #13
                          Re: Empty string comparisons

                          David,

                          To be fair on this question, there were some extra things that I did not
                          post in my example that would probably have affected the outcome. So, you
                          may be right in suspecting it might have been elsewhere. Here is the
                          complete picture:

                          Dim someError(259) As Byte
                          Dim thisString As String
                          Dim ret As Integer
                          ....
                          ....
                          ' This function call is one located in a C++ DLL
                          ' The someError argument had Marshaling setup as follows
                          ' <MarshalAs(Unma nagedType.LPArr ay, SizeConst:=260) ByVal someError As
                          Byte()
                          ret = externalFunctio n(a, b, c, someError)
                          thisString = Encoding.ASCII. GetString((some Error)

                          ' Now the string comparison. It was found that the
                          ' string's return value was "" but its length was 260, and so all
                          ' combinations of string comparison yielded a False in VB and
                          ' that is why I asked my question.

                          It is from my Compact Framework experience in C# that I got into the habit
                          of setting up Byte arrays for returning error strings from external
                          functions in C++ DLLs, due to P/Invoke Marshaling restrictions in the
                          Compact Framework. And that is what I have continued here too.

                          After reading the various responses here, I feel I am now a bit more
                          knowledgeable on string comparisons.

                          Regards,
                          Neville Lang




                          "David Anton" <DavidAnton@dis cussions.micros oft.comwrote in message
                          news:59A9EAC7-EE03-42AF-8D43-EE26682AC303@mi crosoft.com...
                          Your problem probably lies elsewhere - the VB code you posted is the exact
                          equivalent to the C# code.
                          --
                          David Anton
                          Source code converters: Convert between C#, C++, Java, and VB with the most accurate and reliable source code converters

                          Instant C#: VB to C# converter
                          Instant VB: C# to VB converter
                          Instant C++: C#/VB to C++ converter
                          Instant Python: VB to Python converter
                          >
                          >
                          "Neville Lang" wrote:
                          >
                          >Hi all,
                          >>
                          >I am having a memory blank at the moment. I have been writing in C# for a
                          >number of years and now need to do something in VB.NET, so forgive me
                          >such a
                          >primitive question.
                          >>
                          >In C#, I test whether a string has a value or not by the following
                          >syntax:
                          >>
                          >if (thisString.Tri m() == "")
                          >{
                          > // if true
                          > something()
                          >}
                          >else
                          >{
                          > // if false
                          > somethingelse()
                          >}
                          >>
                          >Now, in VB.NET an equivalent does not seem to work:
                          >>
                          >If thisString.Trim () = "" Then
                          > ' if true
                          > something()
                          >Else
                          > ' if false
                          > somethingelse()
                          >End If
                          >>
                          >I cannot get past a False return for the VB comparison. What is the best
                          >method for testing whether a string is empty or null?
                          >>
                          >>
                          >Regards,
                          >Neville Lang
                          >>
                          >>
                          >>

                          Comment

                          • JimmyKoolPantz

                            #14
                            Re: Empty string comparisons

                            There is a new declaration type of "Nullable" in vs2005 that I like to
                            use. For example:

                            Dim ThisString As Nullable(Of String) = Nothing


                            and when you want to check value of ThisString all you have to do is
                            use this syntax:

                            If ThisString.HasV alue = True Then
                            'Do Something
                            End If




                            Neville Lang wrote:
                            Hi all,
                            >
                            I am having a memory blank at the moment. I have been writing in C# for a
                            number of years and now need to do something in VB.NET, so forgive me such a
                            primitive question.
                            >
                            In C#, I test whether a string has a value or not by the following syntax:
                            >
                            if (thisString.Tri m() == "")
                            {
                            // if true
                            something()
                            }
                            else
                            {
                            // if false
                            somethingelse()
                            }
                            >
                            Now, in VB.NET an equivalent does not seem to work:
                            >
                            If thisString.Trim () = "" Then
                            ' if true
                            something()
                            Else
                            ' if false
                            somethingelse()
                            End If
                            >
                            I cannot get past a False return for the VB comparison. What is the best
                            method for testing whether a string is empty or null?
                            >
                            >
                            Regards,
                            Neville Lang

                            Comment

                            • Claes Bergefall

                              #15
                              Re: Empty string comparisons

                              Ahh, yes. The Is operator behaves different from the = operator.
                              VB is hilarious :-)

                              /claes

                              "David Anton" <DavidAnton@dis cussions.micros oft.comwrote in message
                              news:C21FAC70-9BA6-4FE4-A541-6C359ADD3EB4@mi crosoft.com...
                              Yes, but try:
                              Dim s1 As String = ""
                              Dim b1 As Boolean = (s1 Is Nothing)
                              >
                              b1 evaluates to false. Isn't VB fun?!
                              --
                              David Anton
                              Source code converters: Convert between C#, C++, Java, and VB with the most accurate and reliable source code converters

                              Instant C#: VB to C# converter
                              Instant VB: C# to VB converter
                              Instant C++: C#/VB to C++ converter
                              Instant Python: VB to Python converter
                              >
                              >
                              "Claes Bergefall" wrote:
                              >
                              >Actually it does (at least in 2.0). All of the following evaluate to
                              >True:
                              >>
                              >Dim s1 As String = ""
                              >Dim s2 As String = Nothing
                              >Dim b1 As Boolean = (s1 = Nothing)
                              >Dim b2 As Boolean = (s2 = Nothing)
                              >Dim b3 As Boolean = (s1 = "")
                              >Dim b4 As Boolean = (s2 = "")
                              >>
                              >You have to use String.Equals to distinguish between Nothing and an empty
                              >string. Personally I find the VB.NET behaviour annoying and would prefer
                              >that it worked the same as in C#
                              >>
                              > /claes
                              >>
                              >"David Anton" <DavidAnton@dis cussions.micros oft.comwrote in message
                              >news:63186DF 2-8DE7-4EA4-9927-D6A755AD4596@mi crosoft.com...
                              Regarding your first point, VB is even stranger than that:
                              A string set to Nothng (or uninitialized) is regarded as equal to an
                              empty
                              string, but an empty string is *not* regarded as equal to Nothing. C#
                              is
                              consistent in this respect: a null string is a null string and an empty
                              string is an empty string.
                              --
                              David Anton
                              Source code converters: Convert between C#, C++, Java, and VB with the most accurate and reliable source code converters

                              Instant C#: VB to C# converter
                              Instant VB: C# to VB converter
                              Instant C++: C#/VB to C++ converter
                              Instant Python: VB to Python converter
                              >
                              >
                              "Claes Bergefall" wrote:
                              >
                              >The code you have should work just fine. There are other ways to do it
                              >as
                              >well though:
                              >>
                              >1. Use the VB.NET "feature" that null and empty string are considered
                              >equal
                              >when using the = operator, i.e the following statement:
                              >If thisString = "" Then
                              >returns true if thisString is either empty *or* null (beware, this
                              >does
                              >not
                              >work in C#)
                              >2. Use the new String.IsNullOr Empty method (requires .NET 2.0)
                              >>
                              > /claes
                              >>
                              >"Neville Lang" <neville@MAPS_O Nnjlsoftware.co mwrote in message
                              >news:ucODy9$9G HA.3348@TK2MSFT NGP03.phx.gbl.. .
                              Hi all,
                              >
                              I am having a memory blank at the moment. I have been writing in C#
                              for
                              a
                              number of years and now need to do something in VB.NET, so forgive
                              me
                              such
                              a primitive question.
                              >
                              In C#, I test whether a string has a value or not by the following
                              syntax:
                              >
                              if (thisString.Tri m() == "")
                              {
                              // if true
                              something()
                              }
                              else
                              {
                              // if false
                              somethingelse()
                              }
                              >
                              Now, in VB.NET an equivalent does not seem to work:
                              >
                              If thisString.Trim () = "" Then
                              ' if true
                              something()
                              Else
                              ' if false
                              somethingelse()
                              End If
                              >
                              I cannot get past a False return for the VB comparison. What is the
                              best
                              method for testing whether a string is empty or null?
                              >
                              >
                              Regards,
                              Neville Lang
                              >
                              >
                              >>
                              >>
                              >>
                              >>
                              >>
                              >>

                              Comment

                              Working...