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:
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:
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.
>
>
>
>
"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?
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?
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