Re: trim(string) vs string.trim
- From: Jared <Jared@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Sat, 26 Aug 2006 05:54:01 -0700
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.medeiros@xxxxxxxxx> wrote in message
news:1156188881.224330.253450@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
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.
- Follow-Ups:
- Re: trim(string) vs string.trim
- From: Cor Ligthert [MVP]
- Re: trim(string) vs string.trim
- References:
- trim(string) vs string.trim
- From: Terry Olsen
- Re: trim(string) vs string.trim
- From: Branco Medeiros
- Re: trim(string) vs string.trim
- From: Terry Olsen
- trim(string) vs string.trim
- Prev by Date: Re: Help with bitmap DIB
- Next by Date: How can I use a dll with several programs?
- Previous by thread: Re: trim(string) vs string.trim
- Next by thread: Re: trim(string) vs string.trim
- Index(es):
Relevant Pages
|