By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,759 Members | 959 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,759 IT Pros & Developers. It's quick & easy.

trim(string) vs string.trim

P: n/a
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
Share this Question
Share on Google+
22 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a

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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
"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

P: n/a

"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

P: n/a

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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a

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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.