trim(string) vs string.trim

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

    #16
    Re: trim(string) vs string.trim

    Terry,
    Please note before leaving your code in place that the Trim function is
    implemented in the Microsoft.Visua lBasic namespace for backward
    compatability. If you use Reflector you'll find that the internal
    implementation begins with the following check.

    If ((str Is Nothing) OrElse (str.Length = 0)) Then
    Return ""
    End If

    Now, I realize this call does the "work" for you, but, assume you (or
    someone else)wants to convert the project to another .net language. C# for
    instance does not implement a global Trim() method. The developers porting
    your code are then forced** to change every reference to Trim() to either a
    utility function or to the native framework methods. It's best to just
    conform and avoid the backward compatible functions.


    ** The conversion utility may make this change for you.

    "Terry Olsen" wrote:
    Yes, after all the input, I have decided to leave it as String=Trim(Str ing)
    >
    "Branco Medeiros" <branco.medeiro s@gmail.comwrot e in message
    news:1156188881 .224330.253450@ m79g2000cwm.goo glegroups.com.. .

    Terry Olsen wrote:
    I have an app that makes decisions based on string content. I need to
    make
    sure that a string does not contain only spaces or newlines. I am using
    the
    syntax 'Trim(String)" and it works fine. I thought I'd change it to the
    VB
    .NET method "String.Tri m" but that throws an object exception.
    >
    Which brings the question: is it compliant to use Trim(String), or is it
    more within etiquette to use If Not String Is Nothing Then String.Trim?
    The advantage of using Trim instead of String.Trim is exactly that Trim
    will recognize when a String is Nothing and return "" as a result. If
    this is the logic of your application, then instead of doing:

    If SomeStr Is Nothing then
    Value = ""
    'I personally preffer Value = String.Empty
    Else
    Value = String.Trim
    End If

    you could spare the effort and just use Value = Trim(SomeStr)

    On the other hand, if you must know when a passed string is invalid
    (Nothing) then probably checking for Nothing before calling String.Trim
    is the way to go.

    Regards,

    Branco.
    >
    >
    >

    Comment

    • Al Reid

      #17
      Re: trim(string) vs string.trim

      Jared wrote:
      Terry,
      Please note before leaving your code in place that the Trim function
      is implemented in the Microsoft.Visua lBasic namespace for backward
      compatability. If you use Reflector you'll find that the internal
      implementation begins with the following check.
      >
      If ((str Is Nothing) OrElse (str.Length = 0)) Then
      Return ""
      End If
      >
      Now, I realize this call does the "work" for you, but, assume you (or
      someone else)wants to convert the project to another .net language.
      C# for instance does not implement a global Trim() method. The
      developers porting your code are then forced** to change every
      reference to Trim() to either a utility function or to the native
      framework methods. It's best to just conform and avoid the backward
      compatible functions.
      >
      >
      ** The conversion utility may make this change for you.
      >
      "Terry Olsen" wrote:
      >
      Alright, I've been seeing people rant and rave about no using the VisualBasic namespace in a VB application and that it is there for
      backward compatibility (which I don't agree with. I will agree that VisualBasic.Com patibility is). My question is this, why chose
      VB as a language and then go out of your way to avoid using the features built into the language via the VisualBasic namespace. I
      just don't get it. If one wants to write a c# app, why not just do it in c#? Perhaps I don't get it because I have been writing VB
      apps using MS dialects of Basic since 1982. The day I stop using the VisualBasic Namespace (excluding the .Compatibility) is the
      day I stop writing in VB.

      Just My $0.02.
      --
      Al Reid



      Comment

      • Phill W.

        #18
        Re: trim(string) vs string.trim

        Jared wrote:
        Please note before leaving your code in place that the Trim function is
        implemented in the Microsoft.Visua lBasic namespace for backward
        compatability.
        No it's not. Microsoft.Visua lBasic.*Compati bility* contains all the
        archaic stuff, liked fixed-length Strings, that we really /ought/ to
        live without these days, but Microsoft.Visua lBasic is part and parcel of
        the [Visual Basic] language. Just try removing this assembly from your
        system and see how many VB.Net applications still run, even those where
        you've managed to /avoid/ using any of its methods yourself. (Don't
        bother, the answer is none. Build anything in VB.Net and is will be
        dependent on MS.VB).
        Now, I realize this call does the "work" for you, but, assume you (or
        someone else)wants to convert the project to another .net language.
        C# for instance does not implement a global Trim() method.
        It's /not/ a Global Method.
        It's a Framework method, just like any other; it's just that it doesn't
        happen to be part of the System.* hierarchy.
        And there's nothing to stop you Using Microsoft.Visua lBasic in a C#
        program as well.
        (well; at least not on a Windows machine, anyway) ... ;-)

        Regards,
        Phill W.

        Comment

        • Cor Ligthert [MVP]

          #19
          Re: trim(string) vs string.trim

          Jared,

          Just to say it in one line as Phill already wrote, all Visual Basic
          namespace methods (not language parts) works as good as every other Net
          namespace method in C#.

          It has only to be referenced in C#, because the namespaces it is not
          standard referenced in the designer as it is in VB.Net.

          However because that diehard C++ developers want to avoid everything from VB
          as a plague is that seldom be done.

          Most VBNet programmers take everything that gives the quickest results. It
          is just a way of thinking. The quality of those results is in this case the
          same.

          Just as addition,

          Cor

          "Jared" <Jared@discussi ons.microsoft.c omschreef in bericht
          news:F564390B-BE3E-42F6-B1F4-F7CED1E8FCE5@mi crosoft.com...
          Terry,
          Please note before leaving your code in place that the Trim function is
          implemented in the Microsoft.Visua lBasic namespace for backward
          compatability. If you use Reflector you'll find that the internal
          implementation begins with the following check.
          >
          If ((str Is Nothing) OrElse (str.Length = 0)) Then
          Return ""
          End If
          >
          Now, I realize this call does the "work" for you, but, assume you (or
          someone else)wants to convert the project to another .net language. C#
          for
          instance does not implement a global Trim() method. The developers porting
          your code are then forced** to change every reference to Trim() to either
          a
          utility function or to the native framework methods. It's best to just
          conform and avoid the backward compatible functions.
          >
          >
          ** The conversion utility may make this change for you.
          >
          "Terry Olsen" wrote:
          >
          >Yes, after all the input, I have decided to leave it as
          >String=Trim(St ring)
          >>
          >"Branco Medeiros" <branco.medeiro s@gmail.comwrot e in message
          >news:115618888 1.224330.253450 @m79g2000cwm.go oglegroups.com. ..
          >
          Terry Olsen wrote:
          >I have an app that makes decisions based on string content. I need to
          >make
          >sure that a string does not contain only spaces or newlines. I am
          >using
          >the
          >syntax 'Trim(String)" and it works fine. I thought I'd change it to
          >the
          >VB
          >.NET method "String.Tri m" but that throws an object exception.
          >>
          >Which brings the question: is it compliant to use Trim(String), or is
          >it
          >more within etiquette to use If Not String Is Nothing Then
          >String.Trim?
          >
          The advantage of using Trim instead of String.Trim is exactly that Trim
          will recognize when a String is Nothing and return "" as a result. If
          this is the logic of your application, then instead of doing:
          >
          If SomeStr Is Nothing then
          Value = ""
          'I personally preffer Value = String.Empty
          Else
          Value = String.Trim
          End If
          >
          you could spare the effort and just use Value = Trim(SomeStr)
          >
          On the other hand, if you must know when a passed string is invalid
          (Nothing) then probably checking for Nothing before calling String.Trim
          is the way to go.
          >
          Regards,
          >
          Branco.
          >
          >>
          >>
          >>

          Comment

          • Jared

            #20
            Re: trim(string) vs string.trim

            I think you guys are taking me for someone who is advocating that all should
            use C#. I’m not trying to convert anyone, I simply stated that when coding,
            in whatever language you choose, one should try to conform to the standards
            the industry has put in place.

            I can appreciate your frustration, as until recently I developed solely in
            VB. I understand your points, and yes I know that I can use the
            Microsoft.Visua lBasic namespace in C# applications, I’ve never disputed this,
            nor was it ever the topic of discussion.

            My points, with the exception of my backwards compatibility comment, are
            language agnostic and follow the best practices laid out in books such as
            Code Complete and Pragmatic Programmer. I simple stated that you should use
            a utility class and perform your trim there, even if you use the Trim method
            from the Microsoft.Visua lBasic namespace and later I decide to convert it I
            only have to change it in a single location. This saves me from adding an
            import/using statement to each and every object that is using the
            Microsoft.Visua lBasic Trim function. Isn’t the goal to keep the class closed
            for modifications? Why go back and re-work numerous classes for such a small
            change?

            On another similar issue:
            Recently, I was working with a third party GIS mapping application. There
            where few interfaces and/or they did not publicly expose some of the internal
            structures. We were tasked with interacting with the application’s API to
            perform search services through a web service interface. The problem arose
            when we found that we could not gain access to some of the properties using
            C#. Upon inspection of sample services gathered from the applications
            creators we found they were using the late binding features of Visual Basic
            to perform nearly all of the operations. While this wasn’t a huge obstacle,
            we were forced to use create a Visual Basic project to interact with the
            application, either that or use reflection to instantiate internal/friend
            classes of the framework.

            This is the type of thing I’m trying to avoid in the future, the designers
            of the application had the same mentality, it’s easy enough to do using a
            particular languages feature, why change the COM object, we can just force
            them to use Visual Basic or a more elaborate workaround.

            I would love to hear you constructive comments on these subjects. Again,
            try not to turn this into a language discussion.

            Jared


            "Jared" wrote:
            Terry,
            Please note before leaving your code in place that the Trim function is
            implemented in the Microsoft.Visua lBasic namespace for backward
            compatability. If you use Reflector you'll find that the internal
            implementation begins with the following check.
            >
            If ((str Is Nothing) OrElse (str.Length = 0)) Then
            Return ""
            End If
            >
            Now, I realize this call does the "work" for you, but, assume you (or
            someone else)wants to convert the project to another .net language. C# for
            instance does not implement a global Trim() method. The developers porting
            your code are then forced** to change every reference to Trim() to either a
            utility function or to the native framework methods. It's best to just
            conform and avoid the backward compatible functions.
            >
            >
            ** The conversion utility may make this change for you.
            >
            "Terry Olsen" wrote:
            >
            Yes, after all the input, I have decided to leave it as String=Trim(Str ing)

            "Branco Medeiros" <branco.medeiro s@gmail.comwrot e in message
            news:1156188881 .224330.253450@ m79g2000cwm.goo glegroups.com.. .
            >
            Terry Olsen wrote:
            >I have an app that makes decisions based on string content. I need to
            >make
            >sure that a string does not contain only spaces or newlines. I am using
            >the
            >syntax 'Trim(String)" and it works fine. I thought I'd change it to the
            >VB
            >.NET method "String.Tri m" but that throws an object exception.
            >>
            >Which brings the question: is it compliant to use Trim(String), or is it
            >more within etiquette to use If Not String Is Nothing Then String.Trim?
            >
            The advantage of using Trim instead of String.Trim is exactly that Trim
            will recognize when a String is Nothing and return "" as a result. If
            this is the logic of your application, then instead of doing:
            >
            If SomeStr Is Nothing then
            Value = ""
            'I personally preffer Value = String.Empty
            Else
            Value = String.Trim
            End If
            >
            you could spare the effort and just use Value = Trim(SomeStr)
            >
            On the other hand, if you must know when a passed string is invalid
            (Nothing) then probably checking for Nothing before calling String.Trim
            is the way to go.
            >
            Regards,
            >
            Branco.
            >

            Comment

            • Cor Ligthert [MVP]

              #21
              Re: trim(string) vs string.trim

              Jared,

              Who made the standards for the industry those six millions VB users ore the
              fraction of that using C++ and Java. Strange enough needs those
              programlanguage s more books.The writters build have build around that their
              theories, what for me only says something about the limited knowledge of
              those writters. As your text was right, than there should be much more
              semantic from VB in those languages.

              An other main language is Cobol, a while ago it was still the most used
              proffesional programming language I don't know if that is still the same.



              Beside that it seems that *we* are unable to make it clear to you. The
              Microsoft.Visua lBasic namespace is in Net the same as the System.Net.Data
              namespace. It has only a name that not starts with System.Net. Maybe becomes
              it clearer to do (although I doubt that), that everything using those is
              simple in the resulting ils exe (assembly).

              Just my thought reading your message

              Cor

              "Jared" <Jared@discussi ons.microsoft.c omschreef in bericht
              news:9899747E-443E-47BD-9589-260AB7316A1D@mi crosoft.com...
              >I think you guys are taking me for someone who is advocating that all
              >should
              use C#. I'm not trying to convert anyone, I simply stated that when
              coding,
              in whatever language you choose, one should try to conform to the
              standards
              the industry has put in place.
              >
              I can appreciate your frustration, as until recently I developed solely in
              VB. I understand your points, and yes I know that I can use the
              Microsoft.Visua lBasic namespace in C# applications, I've never disputed
              this,
              nor was it ever the topic of discussion.
              >
              My points, with the exception of my backwards compatibility comment, are
              language agnostic and follow the best practices laid out in books such as
              Code Complete and Pragmatic Programmer. I simple stated that you should
              use
              a utility class and perform your trim there, even if you use the Trim
              method
              from the Microsoft.Visua lBasic namespace and later I decide to convert it
              I
              only have to change it in a single location. This saves me from adding an
              import/using statement to each and every object that is using the
              Microsoft.Visua lBasic Trim function. Isn't the goal to keep the class
              closed
              for modifications? Why go back and re-work numerous classes for such a
              small
              change?
              >
              On another similar issue:
              Recently, I was working with a third party GIS mapping application. There
              where few interfaces and/or they did not publicly expose some of the
              internal
              structures. We were tasked with interacting with the application's API to
              perform search services through a web service interface. The problem
              arose
              when we found that we could not gain access to some of the properties
              using
              C#. Upon inspection of sample services gathered from the applications
              creators we found they were using the late binding features of Visual
              Basic
              to perform nearly all of the operations. While this wasn't a huge
              obstacle,
              we were forced to use create a Visual Basic project to interact with the
              application, either that or use reflection to instantiate internal/friend
              classes of the framework.
              >
              This is the type of thing I'm trying to avoid in the future, the designers
              of the application had the same mentality, it's easy enough to do using a
              particular languages feature, why change the COM object, we can just
              force
              them to use Visual Basic or a more elaborate workaround.
              >
              I would love to hear you constructive comments on these subjects. Again,
              try not to turn this into a language discussion.
              >
              Jared
              >
              >
              "Jared" wrote:
              >
              >Terry,
              >Please note before leaving your code in place that the Trim function is
              >implemented in the Microsoft.Visua lBasic namespace for backward
              >compatabilit y. If you use Reflector you'll find that the internal
              >implementati on begins with the following check.
              >>
              >If ((str Is Nothing) OrElse (str.Length = 0)) Then
              > Return ""
              >End If
              >>
              >Now, I realize this call does the "work" for you, but, assume you (or
              >someone else)wants to convert the project to another .net language. C#
              >for
              >instance does not implement a global Trim() method. The developers
              >porting
              >your code are then forced** to change every reference to Trim() to either
              >a
              >utility function or to the native framework methods. It's best to just
              >conform and avoid the backward compatible functions.
              >>
              >>
              >** The conversion utility may make this change for you.
              >>
              >"Terry Olsen" wrote:
              >>
              Yes, after all the input, I have decided to leave it as
              String=Trim(Str ing)
              >
              "Branco Medeiros" <branco.medeiro s@gmail.comwrot e in message
              news:1156188881 .224330.253450@ m79g2000cwm.goo glegroups.com.. .
              >
              Terry Olsen wrote:
              >I have an app that makes decisions based on string content. I need
              >to
              >make
              >sure that a string does not contain only spaces or newlines. I am
              >using
              >the
              >syntax 'Trim(String)" and it works fine. I thought I'd change it to
              >the
              >VB
              >.NET method "String.Tri m" but that throws an object exception.
              >>
              >Which brings the question: is it compliant to use Trim(String), or
              >is it
              >more within etiquette to use If Not String Is Nothing Then
              >String.Trim?
              >
              The advantage of using Trim instead of String.Trim is exactly that
              Trim
              will recognize when a String is Nothing and return "" as a result. If
              this is the logic of your application, then instead of doing:
              >
              If SomeStr Is Nothing then
              Value = ""
              'I personally preffer Value = String.Empty
              Else
              Value = String.Trim
              End If
              >
              you could spare the effort and just use Value = Trim(SomeStr)
              >
              On the other hand, if you must know when a passed string is invalid
              (Nothing) then probably checking for Nothing before calling
              String.Trim
              is the way to go.
              >
              Regards,
              >
              Branco.
              >
              >
              >
              >

              Comment

              • Tom Shelton

                #22
                Re: trim(string) vs string.trim


                Branco Medeiros wrote:
                Terry Olsen wrote:
                I have an app that makes decisions based on string content. I need to make
                sure that a string does not contain only spaces or newlines. I am using the
                syntax 'Trim(String)" and it works fine. I thought I'd change it to the VB
                .NET method "String.Tri m" but that throws an object exception.

                Which brings the question: is it compliant to use Trim(String), or is it
                more within etiquette to use If Not String Is Nothing Then String.Trim?
                >
                The advantage of using Trim instead of String.Trim is exactly that Trim
                will recognize when a String is Nothing and return "" as a result. If
                this is the logic of your application, then instead of doing:
                >
                If SomeStr Is Nothing then
                Value = ""
                'I personally preffer Value = String.Empty
                Else
                Value = String.Trim
                End If
                >
                In C# 2.0, you I would write this:

                public static class StringUtils
                {
                public static string Trim(string str)
                {
                return (str == null) ? string.Empty : str.Trim();
                }
                }

                A nice one liner :)

                --
                Tom Shelton

                Comment

                • Jared

                  #23
                  Re: trim(string) vs string.trim

                  Cor,
                  I'm not, nor was I ever disputing the fact that the Microsoft.Visua lBasic
                  namespace is a part of the .net framework. I'll stand down on this
                  discussion now, it seems that we can agree to disagree on this subject.

                  Jared

                  "Cor Ligthert [MVP]" wrote:
                  Jared,
                  >
                  Who made the standards for the industry those six millions VB users ore the
                  fraction of that using C++ and Java. Strange enough needs those
                  programlanguage s more books.The writters build have build around that their
                  theories, what for me only says something about the limited knowledge of
                  those writters. As your text was right, than there should be much more
                  semantic from VB in those languages.
                  >
                  An other main language is Cobol, a while ago it was still the most used
                  proffesional programming language I don't know if that is still the same.
                  >

                  >
                  Beside that it seems that *we* are unable to make it clear to you. The
                  Microsoft.Visua lBasic namespace is in Net the same as the System.Net.Data
                  namespace. It has only a name that not starts with System.Net. Maybe becomes
                  it clearer to do (although I doubt that), that everything using those is
                  simple in the resulting ils exe (assembly).
                  >
                  Just my thought reading your message
                  >
                  Cor
                  >
                  "Jared" <Jared@discussi ons.microsoft.c omschreef in bericht
                  news:9899747E-443E-47BD-9589-260AB7316A1D@mi crosoft.com...
                  I think you guys are taking me for someone who is advocating that all
                  should
                  use C#. I'm not trying to convert anyone, I simply stated that when
                  coding,
                  in whatever language you choose, one should try to conform to the
                  standards
                  the industry has put in place.

                  I can appreciate your frustration, as until recently I developed solely in
                  VB. I understand your points, and yes I know that I can use the
                  Microsoft.Visua lBasic namespace in C# applications, I've never disputed
                  this,
                  nor was it ever the topic of discussion.

                  My points, with the exception of my backwards compatibility comment, are
                  language agnostic and follow the best practices laid out in books such as
                  Code Complete and Pragmatic Programmer. I simple stated that you should
                  use
                  a utility class and perform your trim there, even if you use the Trim
                  method
                  from the Microsoft.Visua lBasic namespace and later I decide to convert it
                  I
                  only have to change it in a single location. This saves me from adding an
                  import/using statement to each and every object that is using the
                  Microsoft.Visua lBasic Trim function. Isn't the goal to keep the class
                  closed
                  for modifications? Why go back and re-work numerous classes for such a
                  small
                  change?

                  On another similar issue:
                  Recently, I was working with a third party GIS mapping application. There
                  where few interfaces and/or they did not publicly expose some of the
                  internal
                  structures. We were tasked with interacting with the application's API to
                  perform search services through a web service interface. The problem
                  arose
                  when we found that we could not gain access to some of the properties
                  using
                  C#. Upon inspection of sample services gathered from the applications
                  creators we found they were using the late binding features of Visual
                  Basic
                  to perform nearly all of the operations. While this wasn't a huge
                  obstacle,
                  we were forced to use create a Visual Basic project to interact with the
                  application, either that or use reflection to instantiate internal/friend
                  classes of the framework.

                  This is the type of thing I'm trying to avoid in the future, the designers
                  of the application had the same mentality, it's easy enough to do using a
                  particular languages feature, why change the COM object, we can just
                  force
                  them to use Visual Basic or a more elaborate workaround.

                  I would love to hear you constructive comments on these subjects. Again,
                  try not to turn this into a language discussion.

                  Jared


                  "Jared" wrote:
                  Terry,
                  Please note before leaving your code in place that the Trim function is
                  implemented in the Microsoft.Visua lBasic namespace for backward
                  compatability. If you use Reflector you'll find that the internal
                  implementation begins with the following check.
                  >
                  If ((str Is Nothing) OrElse (str.Length = 0)) Then
                  Return ""
                  End If
                  >
                  Now, I realize this call does the "work" for you, but, assume you (or
                  someone else)wants to convert the project to another .net language. C#
                  for
                  instance does not implement a global Trim() method. The developers
                  porting
                  your code are then forced** to change every reference to Trim() to either
                  a
                  utility function or to the native framework methods. It's best to just
                  conform and avoid the backward compatible functions.
                  >
                  >
                  ** The conversion utility may make this change for you.
                  >
                  "Terry Olsen" wrote:
                  >
                  Yes, after all the input, I have decided to leave it as
                  String=Trim(Str ing)

                  "Branco Medeiros" <branco.medeiro s@gmail.comwrot e in message
                  news:1156188881 .224330.253450@ m79g2000cwm.goo glegroups.com.. .
                  >
                  Terry Olsen wrote:
                  >I have an app that makes decisions based on string content. I need
                  >to
                  >make
                  >sure that a string does not contain only spaces or newlines. I am
                  >using
                  >the
                  >syntax 'Trim(String)" and it works fine. I thought I'd change it to
                  >the
                  >VB
                  >.NET method "String.Tri m" but that throws an object exception.
                  >>
                  >Which brings the question: is it compliant to use Trim(String), or
                  >is it
                  >more within etiquette to use If Not String Is Nothing Then
                  >String.Trim?
                  >
                  The advantage of using Trim instead of String.Trim is exactly that
                  Trim
                  will recognize when a String is Nothing and return "" as a result. If
                  this is the logic of your application, then instead of doing:
                  >
                  If SomeStr Is Nothing then
                  Value = ""
                  'I personally preffer Value = String.Empty
                  Else
                  Value = String.Trim
                  End If
                  >
                  you could spare the effort and just use Value = Trim(SomeStr)
                  >
                  On the other hand, if you must know when a passed string is invalid
                  (Nothing) then probably checking for Nothing before calling
                  String.Trim
                  is the way to go.
                  >
                  Regards,
                  >
                  Branco.
                  >

                  >
                  >
                  >

                  Comment

                  Working...