473,387 Members | 1,779 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

trim(string) vs string.trim

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?
Aug 20 '06 #1
22 9681
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?
Aug 21 '06 #2
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.comwrote in message
news:24**********************************@microsof t.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?

Aug 21 '06 #3
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" <lo*****@nospam.nospamschreef in bericht
news:%2****************@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.comwrote in message
news:24**********************************@microsof t.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?


Aug 21 '06 #4

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.

Aug 21 '06 #5
Yes, after all the input, I have decided to leave it as String=Trim(String)

"Branco Medeiros" <br*************@gmail.comwrote in message
news:11**********************@m79g2000cwm.googlegr oups.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.

Aug 22 '06 #6
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.
Aug 22 '06 #7
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

"Phill W." <p-.-a-.-w-a-r-d@o-p-e-n-.-a-c-.-u-kschreef in bericht
news:ec**********@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.

Aug 22 '06 #8
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.

"Phill W." <p-.-a-.-w-a-r-d@o-p-e-n-.-a-c-.-u-kwrote in message
news:ec**********@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.

Aug 22 '06 #9
"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
news:uM**************@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
Aug 22 '06 #10

"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
news:eu*************@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
Aug 22 '06 #11

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" <lo*****@nospam.nospamschreef in bericht
news:%2****************@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.comwrote in message
news:24**********************************@microsof t.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.

Aug 22 '06 #12
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" <Ma***********@ThePentagon.comschreef in bericht
news:11**********************@p79g2000cwp.googlegr oups.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" <lo*****@nospam.nospamschreef in bericht
news:%2****************@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.comwrote in message
news:24**********************************@microsof t.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.

Aug 22 '06 #13
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" <Ma***********@ThePentagon.comschreef in bericht
news:11**********************@p79g2000cwp.googlegr oups.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" <lo*****@nospam.nospamschreef in bericht
news:%2****************@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.comwrote in message
news:24**********************************@micro soft.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
Aug 23 '06 #14
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

"Jim Wooley" <ji*************@hotmail.comschreef in bericht
news:24*************************@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" <Ma***********@ThePentagon.comschreef in bericht
news:11**********************@p79g2000cwp.googleg roups.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" <lo*****@nospam.nospamschreef in bericht
news:%2****************@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.comwrote in message
news:24**********************************@micr osoft.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


Aug 23 '06 #15
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" <br*************@gmail.comwrote in message
news:11**********************@m79g2000cwm.googlegr oups.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.


Aug 25 '06 #16
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:
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

Aug 25 '06 #17
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.
Aug 25 '06 #18
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" <Ja***@discussions.microsoft.comschreef in bericht
news:F5**********************************@microsof t.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" <br*************@gmail.comwrote in message
news:11**********************@m79g2000cwm.googleg roups.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.



Aug 25 '06 #19
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" <br*************@gmail.comwrote in message
news:11**********************@m79g2000cwm.googlegr oups.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.
>
Aug 26 '06 #20
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.comschreef in bericht
news:98**********************************@microsof t.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" <br*************@gmail.comwrote in message
news:11**********************@m79g2000cwm.googlegr oups.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.


Aug 26 '06 #21

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.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

Aug 26 '06 #22
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

"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.comschreef in bericht
news:98**********************************@microsof t.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" <br*************@gmail.comwrote in message
news:11**********************@m79g2000cwm.googlegr oups.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.
>



Aug 26 '06 #23

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: Simon | last post by:
Hi, I have written a function to trim char *, but I have been told that my way could be dangerous and that I should use memmove(...) instead. but I am not sure why my code could be 'dangerous'...
17
by: Chad Myers | last post by:
I've been perf testing an application of mine and I've noticed that there are a lot (and I mean A LOT -- megabytes and megabytes of 'em) System.String instances being created. I've done some...
11
by: Darren Anderson | last post by:
I have a function that I've tried using in an if then statement and I've found that no matter how much reworking I do with the code, the expected result is incorrect. the code: If Not...
7
by: Sascha Herpers | last post by:
Hi, what is the difference between the trim function and the trim String-member? As far as I see it, both return the trimmed string and leave the original string unaltered. Is any of the two...
5
by: Pete | last post by:
How do you get rid of leading white spaces in a string in vb.net? From http://www.developmentnow.com/g/38_2005_9_1_0_0/dotnet-languages-vb.ht Posted via DevelopmentNow.com Group...
10
by: klineb | last post by:
Good Day, I have written and utility to convert our DOS COBOL data files to a SQL Server database. Part of the process requires parsing each line into a sql statement and validting the data to...
3
by: Pascal | last post by:
bonjour hello I would like to trim a string of all its white spaces so i used myString.trim() but it doesn't work as supposed : unsecable space are remaining in the middle of my string... i...
31
by: rkk | last post by:
Hi, I've written a small trim function to trim away the whitespaces in a given string. It works well with solaris forte cc compiler, but on mingw/cygwin gcc it isn't. Here is the code: char...
1
by: Sankalp | last post by:
Hi, I am using VB 2005. My application has many data bound controls. The connection is stored in the app.config file. I want the application to start with a default connection string and while...
5
by: =?Utf-8?B?c2VlbWE=?= | last post by:
Hi, I have a string line = " DOL : 12/04/09 " I want to remove the white spaces and to get the string like line = "DOL : 12/04/09" ; i have th e following coding but...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.