Home All Groups Group Topic Archive Search About

trim(string) vs string.trim

Author
20 Aug 2006 11:30 PM
Terry Olsen
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.Trim" 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?

Author
21 Aug 2006 12:19 AM
Jared
Terry,
You should always check your parameters/variables for null references
whenever there is doubt. 

You can use the negating logic if you prefer, its just not as straight
forward (it is here, I just avoid negating logic whenever possible)
If Not Is Nothing Then
  stringVariable = stringVariable.Trim
End If

If stringVariable is nothing then
  stringVariable = string.empty
Else
  stringVariable = stringVariable.Trim
End If

Whatever you decide, just be consistient.

Show quoteHide quote
"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.Trim" 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?
>
>
>
Author
21 Aug 2006 2:10 PM
Claes Bergefall
Don't forget the new IsNot operator that was introduced in 2.0. It's a bit
easier to read imo.

If stringVariable IsNot Nothing Then
    stringVariable = stringVariable.Trim
End If

   /claes

Show quoteHide quote
"Jared" <Ja***@discussions.microsoft.com> wrote in message
news:246C89B1-5305-441E-A74C-263551BA6ECC@microsoft.com...
> Terry,
> You should always check your parameters/variables for null references
> whenever there is doubt.
>
> You can use the negating logic if you prefer, its just not as straight
> forward (it is here, I just avoid negating logic whenever possible)
> If Not Is Nothing Then
>  stringVariable = stringVariable.Trim
> End If
>
> If stringVariable is nothing then
>  stringVariable = string.empty
> Else
>  stringVariable = stringVariable.Trim
> End If
>
> Whatever you decide, just be consistient.
>
> "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.Trim" 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?
>>
>>
>>
Author
21 Aug 2006 3:35 PM
Cor Ligthert [MVP]
Claes,

> Don't forget the new IsNot operator that was introduced in 2.0. It's a bit
> easier to read imo.

Maybe for native English people, I find it terrible to read.

Cor

Show quoteHide quote
"Claes Bergefall" <louplou@nospam.nospam> schreef in bericht
news:%23SQqlsSxGHA.4752@TK2MSFTNGP02.phx.gbl...
> Don't forget the new IsNot operator that was introduced in 2.0. It's a bit
> easier to read imo.
>
> If stringVariable IsNot Nothing Then
>    stringVariable = stringVariable.Trim
> End If
>
>   /claes
>
> "Jared" <Ja***@discussions.microsoft.com> wrote in message
> news:246C89B1-5305-441E-A74C-263551BA6ECC@microsoft.com...
>> Terry,
>> You should always check your parameters/variables for null references
>> whenever there is doubt.
>>
>> You can use the negating logic if you prefer, its just not as straight
>> forward (it is here, I just avoid negating logic whenever possible)
>> If Not Is Nothing Then
>>  stringVariable = stringVariable.Trim
>> End If
>>
>> If stringVariable is nothing then
>>  stringVariable = string.empty
>> Else
>>  stringVariable = stringVariable.Trim
>> End If
>>
>> Whatever you decide, just be consistient.
>>
>> "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.Trim" 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?
>>>
>>>
>>>
>
>
Author
22 Aug 2006 12:35 PM
Phill W.
Cor Ligthert [MVP] wrote:
> Claes,
>
>> Don't forget the new IsNot operator that was introduced in 2.0. It's a bit
>> easier to read imo.
>
> Maybe for native English people, I find it terrible to read.

Cor,

I *am* English and I think the "Eye-Snot" operator is appalling.

I've been using something like

Function IsSomething( ByVal oThing As Object ) As Boolean
    Return Not (oThing Is Nothing )
End Function

(in various guises) for more years than I like to think about and have
no intention of changing.

OK, there's an overhead in calling a function, but any half-decent
compiler would optimise that out.

Regards,
    Phill  W.
Author
22 Aug 2006 1:05 PM
Cor Ligthert [MVP]
Phill,

In your sample it is a boolean.

My is told that English people seem to use IsNot Nothing when it is
Something.

I prefer "something" because I hate those possible constructions as it is in
VB.Net as
If Not (Not ObjectAddress IsNot Nothing) what is a valid instruction to
return a boolean if that address is something.

But as me is told is this for English people easy readable.

Not all spoken language are always algabraic correct do you know.

Cor

Show quoteHide quote
"Phill W." <p-.-a-.-w-a-r-d@o-p-e-n-.-a-c-.-u-k> schreef in bericht
news:ecetn2$p2f$1@south.jnrs.ja.net...
> Cor Ligthert [MVP] wrote:
>> Claes,
>>
>>> Don't forget the new IsNot operator that was introduced in 2.0. It's a
>>> bit easier to read imo.
>>
>> Maybe for native English people, I find it terrible to read.
>
> Cor,
>
> I *am* English and I think the "Eye-Snot" operator is appalling.
>
> I've been using something like
>
> Function IsSomething( ByVal oThing As Object ) As Boolean
>    Return Not (oThing Is Nothing )
> End Function
>
> (in various guises) for more years than I like to think about and have no
> intention of changing.
>
> OK, there's an overhead in calling a function, but any half-decent
> compiler would optimise that out.
>
> Regards,
>    Phill  W.
Author
22 Aug 2006 2:48 PM
Claes Bergefall
"Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> wrote in message
news:eutCIuexGHA.980@TK2MSFTNGP04.phx.gbl...
> Phill,
>
> In your sample it is a boolean.
>
> My is told that English people seem to use IsNot Nothing when it is
> Something.
>
> I prefer "something" because I hate those possible constructions as it is
> in VB.Net as
> If Not (Not ObjectAddress IsNot Nothing) what is a valid instruction to
> return a boolean if that address is something.

Now you're abusing the language :-)
(And you failed my code review ;-))

The above is eqvivalent to:
If ObjectAddress IsNot Nothing
or
If Not ObjectAddress Is Nothing

Personally I find the first form easier to read (and I'm not English). A
'Something' keyword would be nice though...

    /claes
Author
22 Aug 2006 1:07 PM
Terry Olsen
Ok, not being formally trained in the art of coding, what exactly is
apalling about IsNot?  It seems like a perfectly valid and logical option to
me.  Your method is like making 3 left turns to get to the store when a
single right turn would have gotten you there, simply because you don't like
right turns.

Show quoteHide quote
"Phill W." <p-.-a-.-w-a-r-d@o-p-e-n-.-a-c-.-u-k> wrote in message
news:ecetn2$p2f$1@south.jnrs.ja.net...
> Cor Ligthert [MVP] wrote:
>> Claes,
>>
>>> Don't forget the new IsNot operator that was introduced in 2.0. It's a
>>> bit easier to read imo.
>>
>> Maybe for native English people, I find it terrible to read.
>
> Cor,
>
> I *am* English and I think the "Eye-Snot" operator is appalling.
>
> I've been using something like
>
> Function IsSomething( ByVal oThing As Object ) As Boolean
>    Return Not (oThing Is Nothing )
> End Function
>
> (in various guises) for more years than I like to think about and have no
> intention of changing.
>
> OK, there's an overhead in calling a function, but any half-decent
> compiler would optimise that out.
>
> Regards,
>    Phill  W.
Author
22 Aug 2006 2:38 PM
Claes Bergefall
"Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> wrote in message
news:uMl80cTxGHA.4972@TK2MSFTNGP05.phx.gbl...
> Claes,
>
>> Don't forget the new IsNot operator that was introduced in 2.0. It's a
>> bit easier to read imo.
>
> Maybe for native English people, I find it terrible to read.
>
> Cor
>

Well, I'm Swedish and I find it a lot better than using 'If Not ... Is
Nothing...'. That just looks weird. Until they add support for 'If ... Is
Something' I'm sticking with the IsNot operator

  /claes
Author
22 Aug 2006 2:57 PM
Brian Tkatch
Cor Ligthert [MVP] wrote:
Show quoteHide quote
> Claes,
>
> > Don't forget the new IsNot operator that was introduced in 2.0. It's a bit
> > easier to read imo.
>
> Maybe for native English people, I find it terrible to read.
>
> Cor
>
> "Claes Bergefall" <louplou@nospam.nospam> schreef in bericht
> news:%23SQqlsSxGHA.4752@TK2MSFTNGP02.phx.gbl...
> > Don't forget the new IsNot operator that was introduced in 2.0. It's a bit
> > easier to read imo.
> >
> > If stringVariable IsNot Nothing Then
> >    stringVariable = stringVariable.Trim
> > End If
> >
> >   /claes
> >
> > "Jared" <Ja***@discussions.microsoft.com> wrote in message
> > news:246C89B1-5305-441E-A74C-263551BA6ECC@microsoft.com...
> >> Terry,
> >> You should always check your parameters/variables for null references
> >> whenever there is doubt.
> >>
> >> You can use the negating logic if you prefer, its just not as straight
> >> forward (it is here, I just avoid negating logic whenever possible)
> >> If Not Is Nothing Then
> >>  stringVariable = stringVariable.Trim
> >> End If
> >>
> >> If stringVariable is nothing then
> >>  stringVariable = string.empty
> >> Else
> >>  stringVariable = stringVariable.Trim
> >> End If
> >>
> >> Whatever you decide, just be consistient.
> >>
> >> "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.Trim" 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 term closely follows SQL. Is SQL, the IS operator supports NOT, as
opposed to =, which does not support NOT.

Thus A = B and NOT (A = B), but A NOT = B is invalid.

However, IS does support it so: A IS NULL, NOT A IS NULL, and A IS NOT
NULL, are all valid.

Personally, i welcome the IsNot operator. Being "Is" is a special case
(checking the meta-data as opposed to the data itself) having it's own
negativity operator is fine, especially when it sort of follows a well
known SQL standard (for checking meta-data).

B.
Author
22 Aug 2006 3:44 PM
Cor Ligthert [MVP]
Brian, Terry, Claes,

The "IsNot" is one of my Carthago's.

Even this would be better in my idea.

\\\
Dim IsNotYetInstanced As Object
Dim myarray As ArrayList
If myarray Is IsNotYetInstanced Then
            MessageBox.Show("I am not something")
End If
///

This can every kid make, the same as probably is done.

With Jay I would like if there was something as "Something" to show that the
address part of an object was not empty.

Cor


Show quoteHide quote
"Brian Tkatch" <Maxwell_Sm***@ThePentagon.com> schreef in bericht
news:1156258659.903839.259580@p79g2000cwp.googlegroups.com...
>
> Cor Ligthert [MVP] wrote:
>> Claes,
>>
>> > Don't forget the new IsNot operator that was introduced in 2.0. It's a
>> > bit
>> > easier to read imo.
>>
>> Maybe for native English people, I find it terrible to read.
>>
>> Cor
>>
>> "Claes Bergefall" <louplou@nospam.nospam> schreef in bericht
>> news:%23SQqlsSxGHA.4752@TK2MSFTNGP02.phx.gbl...
>> > Don't forget the new IsNot operator that was introduced in 2.0. It's a
>> > bit
>> > easier to read imo.
>> >
>> > If stringVariable IsNot Nothing Then
>> >    stringVariable = stringVariable.Trim
>> > End If
>> >
>> >   /claes
>> >
>> > "Jared" <Ja***@discussions.microsoft.com> wrote in message
>> > news:246C89B1-5305-441E-A74C-263551BA6ECC@microsoft.com...
>> >> Terry,
>> >> You should always check your parameters/variables for null references
>> >> whenever there is doubt.
>> >>
>> >> You can use the negating logic if you prefer, its just not as straight
>> >> forward (it is here, I just avoid negating logic whenever possible)
>> >> If Not Is Nothing Then
>> >>  stringVariable = stringVariable.Trim
>> >> End If
>> >>
>> >> If stringVariable is nothing then
>> >>  stringVariable = string.empty
>> >> Else
>> >>  stringVariable = stringVariable.Trim
>> >> End If
>> >>
>> >> Whatever you decide, just be consistient.
>> >>
>> >> "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.Trim" 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 term closely follows SQL. Is SQL, the IS operator supports NOT, as
> opposed to =, which does not support NOT.
>
> Thus A = B and NOT (A = B), but A NOT = B is invalid.
>
> However, IS does support it so: A IS NULL, NOT A IS NULL, and A IS NOT
> NULL, are all valid.
>
> Personally, i welcome the IsNot operator. Being "Is" is a special case
> (checking the meta-data as opposed to the data itself) having it's own
> negativity operator is fine, especially when it sort of follows a well
> known SQL standard (for checking meta-data).
>
> B.
>
Author
23 Aug 2006 1:07 PM
Jim Wooley
With extension methods in VB9, you will be able to do something like:

<Extension()> _
Public Module CustomExtensions
  <Extension()>  _
  Public Function IsSomething(byVal target as Object) as Boolean
     Return Not target Is Nothing
  End Function
End Module

Then to consume it, as long as your CustomExtensions are in scope, you will
be able to take any object and call it's extension method IsSomething as
follows:

dim foo as Object
If foo.IsSomething
   'Do Work
End If

Note, VB9 is still under development and the syntax is subject to change
before release in Orcas, but it does give you an idea of what you can do.
Be careful, extension methods can be is a very big gun, with which you can
easily shoot yourself in the foot.

Jim Wooley
http://devauthority.com/blogs/jwooley

Show quoteHide quote
> Brian, Terry, Claes,
>
> The "IsNot" is one of my Carthago's.
>
> Even this would be better in my idea.
>
> \\\
> Dim IsNotYetInstanced As Object
> Dim myarray As ArrayList
> If myarray Is IsNotYetInstanced Then
> MessageBox.Show("I am not something")
> End If
> ///
> This can every kid make, the same as probably is done.
>
> With Jay I would like if there was something as "Something" to show
> that the address part of an object was not empty.
>
> Cor
>
> "Brian Tkatch" <Maxwell_Sm***@ThePentagon.com> schreef in bericht
> news:1156258659.903839.259580@p79g2000cwp.googlegroups.com...
>
>> Cor Ligthert [MVP] wrote:
>>
>>> Claes,
>>>
>>>> Don't forget the new IsNot operator that was introduced in 2.0.
>>>> It's a
>>>> bit
>>>> easier to read imo.
>>> Maybe for native English people, I find it terrible to read.
>>>
>>> Cor
>>>
>>> "Claes Bergefall" <louplou@nospam.nospam> schreef in bericht
>>> news:%23SQqlsSxGHA.4752@TK2MSFTNGP02.phx.gbl...
>>>
>>>> Don't forget the new IsNot operator that was introduced in 2.0.
>>>> It's a
>>>> bit
>>>> easier to read imo.
>>>> If stringVariable IsNot Nothing Then
>>>> stringVariable = stringVariable.Trim
>>>> End If
>>>> /claes
>>>>
>>>> "Jared" <Ja***@discussions.microsoft.com> wrote in message
>>>> news:246C89B1-5305-441E-A74C-263551BA6ECC@microsoft.com...
>>>>
>>>>> Terry,
>>>>> You should always check your parameters/variables for null
>>>>> references
>>>>> whenever there is doubt.
>>>>> You can use the negating logic if you prefer, its just not as
>>>>> straight
>>>>> forward (it is here, I just avoid negating logic whenever
>>>>> possible)
>>>>> If Not Is Nothing Then
>>>>> stringVariable = stringVariable.Trim
>>>>> End If
>>>>> If stringVariable is nothing then
>>>>> stringVariable = string.empty
>>>>> Else
>>>>> stringVariable = stringVariable.Trim
>>>>> End If
>>>>> Whatever you decide, just be consistient.
>>>>>
>>>>> "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.Trim" 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 term closely follows SQL. Is SQL, the IS operator supports NOT,
>> as opposed to =, which does not support NOT.
>>
>> Thus A = B and NOT (A = B), but A NOT = B is invalid.
>>
>> However, IS does support it so: A IS NULL, NOT A IS NULL, and A IS
>> NOT NULL, are all valid.
>>
>> Personally, i welcome the IsNot operator. Being "Is" is a special
>> case (checking the meta-data as opposed to the data itself) having
>> it's own negativity operator is fine, especially when it sort of
>> follows a well known SQL standard (for checking meta-data).
>>
>> B.
>>
Jim Wooley
http://devauthority.com/blogs/jwooley/default.aspx
Author
23 Aug 2006 1:48 PM
Cor Ligthert [MVP]
Jim,

I don't want IsSomething, that are solutions thought by developers inside
their own community closing the doors for the world. The same for me as not
using the "+" in programs but creating a function for that IsPlus(ValueA,
ValueB).

It has to be

If myObjectAdress is Something

Cor

Show quoteHide quote
"Jim Wooley" <jimNOSPAMwooley@hotmail.com> schreef in bericht
news:24f81e8fbd448c894aaa4527f5f@msnews.microsoft.com...
> With extension methods in VB9, you will be able to do something like:
>
> <Extension()> _
> Public Module CustomExtensions
>  <Extension()>  _
>  Public Function IsSomething(byVal target as Object) as Boolean
>     Return Not target Is Nothing
>  End Function
> End Module
>
> Then to consume it, as long as your CustomExtensions are in scope, you
> will be able to take any object and call it's extension method IsSomething
> as follows:
>
> dim foo as Object
> If foo.IsSomething
>   'Do Work
> End If
>
> Note, VB9 is still under development and the syntax is subject to change
> before release in Orcas, but it does give you an idea of what you can do.
> Be careful, extension methods can be is a very big gun, with which you can
> easily shoot yourself in the foot.
>
> Jim Wooley
> http://devauthority.com/blogs/jwooley
>
>> Brian, Terry, Claes,
>>
>> The "IsNot" is one of my Carthago's.
>>
>> Even this would be better in my idea.
>>
>> \\\
>> Dim IsNotYetInstanced As Object
>> Dim myarray As ArrayList
>> If myarray Is IsNotYetInstanced Then
>> MessageBox.Show("I am not something")
>> End If
>> ///
>> This can every kid make, the same as probably is done.
>>
>> With Jay I would like if there was something as "Something" to show
>> that the address part of an object was not empty.
>>
>> Cor
>>
>> "Brian Tkatch" <Maxwell_Sm***@ThePentagon.com> schreef in bericht
>> news:1156258659.903839.259580@p79g2000cwp.googlegroups.com...
>>
>>> Cor Ligthert [MVP] wrote:
>>>
>>>> Claes,
>>>>
>>>>> Don't forget the new IsNot operator that was introduced in 2.0.
>>>>> It's a
>>>>> bit
>>>>> easier to read imo.
>>>> Maybe for native English people, I find it terrible to read.
>>>>
>>>> Cor
>>>>
>>>> "Claes Bergefall" <louplou@nospam.nospam> schreef in bericht
>>>> news:%23SQqlsSxGHA.4752@TK2MSFTNGP02.phx.gbl...
>>>>
>>>>> Don't forget the new IsNot operator that was introduced in 2.0.
>>>>> It's a
>>>>> bit
>>>>> easier to read imo.
>>>>> If stringVariable IsNot Nothing Then
>>>>> stringVariable = stringVariable.Trim
>>>>> End If
>>>>> /claes
>>>>>
>>>>> "Jared" <Ja***@discussions.microsoft.com> wrote in message
>>>>> news:246C89B1-5305-441E-A74C-263551BA6ECC@microsoft.com...
>>>>>
>>>>>> Terry,
>>>>>> You should always check your parameters/variables for null
>>>>>> references
>>>>>> whenever there is doubt.
>>>>>> You can use the negating logic if you prefer, its just not as
>>>>>> straight
>>>>>> forward (it is here, I just avoid negating logic whenever
>>>>>> possible)
>>>>>> If Not Is Nothing Then
>>>>>> stringVariable = stringVariable.Trim
>>>>>> End If
>>>>>> If stringVariable is nothing then
>>>>>> stringVariable = string.empty
>>>>>> Else
>>>>>> stringVariable = stringVariable.Trim
>>>>>> End If
>>>>>> Whatever you decide, just be consistient.
>>>>>>
>>>>>> "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.Trim" 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 term closely follows SQL. Is SQL, the IS operator supports NOT,
>>> as opposed to =, which does not support NOT.
>>>
>>> Thus A = B and NOT (A = B), but A NOT = B is invalid.
>>>
>>> However, IS does support it so: A IS NULL, NOT A IS NULL, and A IS
>>> NOT NULL, are all valid.
>>>
>>> Personally, i welcome the IsNot operator. Being "Is" is a special
>>> case (checking the meta-data as opposed to the data itself) having
>>> it's own negativity operator is fine, especially when it sort of
>>> follows a well known SQL standard (for checking meta-data).
>>>
>>> B.
>>>
> Jim Wooley
> http://devauthority.com/blogs/jwooley/default.aspx
>
>
Author
21 Aug 2006 7:34 PM
Branco Medeiros
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.Trim" 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.
Author
22 Aug 2006 1:30 AM
Terry Olsen
Yes, after all the input, I have decided to leave it as String=Trim(String)

Show quoteHide quote
"Branco Medeiros" <branco.medei***@gmail.com> wrote in message
news:1156188881.224330.253450@m79g2000cwm.googlegroups.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.Trim" 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.
>
Author
25 Aug 2006 11:25 AM
Jared
Terry,
Please note before leaving your code in place that the Trim function is
implemented in the Microsoft.VisualBasic 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.

Show quoteHide quote
"Terry Olsen" wrote:

> Yes, after all the input, I have decided to leave it as String=Trim(String)
>
> "Branco Medeiros" <branco.medei***@gmail.com> wrote in message
> news:1156188881.224330.253450@m79g2000cwm.googlegroups.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.Trim" 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.
> >
>
>
>
Author
25 Aug 2006 11:48 AM
Al Reid
Jared wrote:
Show quoteHide quote
> Terry,
> Please note before leaving your code in place that the Trim function
> is implemented in the Microsoft.VisualBasic 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.Compatibility 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
Author
25 Aug 2006 12:32 PM
Phill W.
Jared wrote:

> Please note before leaving your code in place that the Trim function is
> implemented in the Microsoft.VisualBasic namespace for backward
> compatability. 

No it's not.  Microsoft.VisualBasic.*Compatibility* contains all the
archaic stuff, liked fixed-length Strings, that we really /ought/ to
live without these days, but Microsoft.VisualBasic 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.VisualBasic in a C#
program as well.
(well; at least not on a Windows machine, anyway) ...   ;-)

Regards,
    Phill  W.
Author
25 Aug 2006 3:49 PM
Cor Ligthert [MVP]
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

Show quoteHide quote
"Jared" <Ja***@discussions.microsoft.com> schreef in bericht
news:F564390B-BE3E-42F6-B1F4-F7CED1E8FCE5@microsoft.com...
> Terry,
> Please note before leaving your code in place that the Trim function is
> implemented in the Microsoft.VisualBasic 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(String)
>>
>> "Branco Medeiros" <branco.medei***@gmail.com> wrote in message
>> news:1156188881.224330.253450@m79g2000cwm.googlegroups.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.Trim" 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.
>> >
>>
>>
>>
Author
26 Aug 2006 12:54 PM
Jared
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.VisualBasic 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.VisualBasic 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.VisualBasic 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


Show quoteHide quote
"Jared" wrote:

> Terry,
> Please note before leaving your code in place that the Trim function is
> implemented in the Microsoft.VisualBasic 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(String)
> >
> > "Branco Medeiros" <branco.medei***@gmail.com> wrote in message
> > news:1156188881.224330.253450@m79g2000cwm.googlegroups.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.Trim" 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.
> > >
> >
> >
> >
Author
26 Aug 2006 3:05 PM
Cor Ligthert [MVP]
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
programlanguages 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.

http://www.levenez.com/lang/history.html#01

Beside that it seems that *we* are unable to make it clear to you. The
Microsoft.VisualBasic 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

Show quoteHide quote
"Jared" <Ja***@discussions.microsoft.com> schreef in bericht
news:9899747E-443E-47BD-9589-260AB7316A1D@microsoft.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.VisualBasic 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.VisualBasic 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.VisualBasic 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.VisualBasic 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(String)
>> >
>> > "Branco Medeiros" <branco.medei***@gmail.com> wrote in message
>> > news:1156188881.224330.253450@m79g2000cwm.googlegroups.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.Trim" 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.
>> > >
>> >
>> >
>> >
Author
26 Aug 2006 5:34 PM
Jared
Cor,
I'm not, nor was I ever disputing the fact that the Microsoft.VisualBasic
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

Show quoteHide quote
"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
> programlanguages 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.
>
> http://www.levenez.com/lang/history.html#01
>
> Beside that it seems that *we* are unable to make it clear to you. The
> Microsoft.VisualBasic 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" <Ja***@discussions.microsoft.com> schreef in bericht
> news:9899747E-443E-47BD-9589-260AB7316A1D@microsoft.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.VisualBasic 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.VisualBasic 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.VisualBasic 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.VisualBasic 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(String)
> >> >
> >> > "Branco Medeiros" <branco.medei***@gmail.com> wrote in message
> >> > news:1156188881.224330.253450@m79g2000cwm.googlegroups.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.Trim" 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.
> >> > >
> >> >
> >> >
> >> >
>
>
>
Author
26 Aug 2006 3:31 PM
Tom Shelton
Branco Medeiros wrote:
Show quoteHide quote
> 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.Trim" 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