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

not or <>?

P: n/a
which is faster?

If strMessage <> String.Empty Then

End If

or

If Not strMessage Is Nothing Then

End If
Nov 20 '05 #1
Share this Question
Share on Google+
16 Replies


P: n/a
Kerberoz,

Comparing against string.empty is slower as this invokes a call to StrCmp to
compare the two strings and return the result. Take a look at the following
IL for If strMessage <> String.Empty

IL_0008: call int32
[Microsoft.VisualBasic]Microsoft.VisualBasic.CompilerServices.StringType: :St
rCmp(string,

string,

bool)

Cheers,
Lubos
"kerberoz" <de****@hotmail.com> wrote in message
news:eX**************@TK2MSFTNGP11.phx.gbl...
which is faster?

If strMessage <> String.Empty Then

End If

or

If Not strMessage Is Nothing Then

End If

Nov 20 '05 #2

P: n/a
I would point out that these two equality test are doing very different
things.

If strMessage <> String.Empty Then ' This tests for a zero length string

if If Not strMessage Is Nothing Then ' This tests to see if a reference is
associated with this identifier.

Apples and Pears !
Regards - OHM

kerberoz wrote:
which is faster?

If strMessage <> String.Empty Then

End If

or

If Not strMessage Is Nothing Then

End If


--
Best Regards - OHM

O_H_M{at}BTInternet{dot}com
Nov 20 '05 #3

P: n/a
Cor
Hi Kerberoz

In addition to OHT

In this newsgroup we mostly advice to use
strMessage <> ""
or seldom
strMessage <> nothing
but never
strMessage <> String.Empty

Cor

which is faster?

If strMessage <> String.Empty Then

End If

or

If Not strMessage Is Nothing Then

End If

Nov 20 '05 #4

P: n/a
kerberoz,
You're testing two different things (empty string and a null reference).

Here is the technique I use to test for both conditions.

-------------------

' Test for Valid String
If (not szMessage Is Nothing) andalso (szMessage.Length > 0) then

' Do stuff for the valid string.

End If

' Test for invalid string
If (szMessage Is Nothing) orelse (szMessage.Length = 0) then

' Do stuff for the invalid string.

End If
-------------------

AFAIK, "szMessage.Length > 0" is faster than "szMesage <> String.Empty"
HTH,

Trev.

"kerberoz" <de****@hotmail.com> wrote in message
news:eX**************@TK2MSFTNGP11.phx.gbl...
which is faster?

If strMessage <> String.Empty Then

End If

or

If Not strMessage Is Nothing Then

End If

Nov 20 '05 #5

P: n/a
kerberoz,
Does it really matter?

In addition to the others comments about the differences between what you
are actually comparing.

I normally write semantically (full OOP) correct programs, then when a
specific routine has proven to have a performance issue, via profiling. I
look at optimizing that specific routine.

Hope this helps
Jay
"kerberoz" <de****@hotmail.com> wrote in message
news:eX**************@TK2MSFTNGP11.phx.gbl...
which is faster?

If strMessage <> String.Empty Then

End If

or

If Not strMessage Is Nothing Then

End If

Nov 20 '05 #6

P: n/a
"Cor" <no*@non.com> wrote in message
news:u5**************@TK2MSFTNGP10.phx.gbl...
Hi Kerberoz

In addition to OHT

In this newsgroup we mostly advice to use
strMessage <> ""
or seldom
strMessage <> nothing
but never
strMessage <> String.Empty

Cor


What's wrong with strMessage <> String.Empty?

Just curious.

Erik
Nov 20 '05 #7

P: n/a
1.) Nothing wrong with strMessage <> String.Empty,

2.) but lots wrong with strMessage <> nothing if you think you are checking
for an empty string

OHM

Erik Frey wrote:
"Cor" <no*@non.com> wrote in message
news:u5**************@TK2MSFTNGP10.phx.gbl...
Hi Kerberoz

In addition to OHT

In this newsgroup we mostly advice to use
strMessage <> ""
or seldom
strMessage <> nothing
but never
strMessage <> String.Empty

Cor


What's wrong with strMessage <> String.Empty?

Just curious.

Erik


--
Best Regards - OHM

O_H_M{at}BTInternet{dot}com
Nov 20 '05 #8

P: n/a
> 2.) but lots wrong with strMessage <> nothing
if you think you are checking for an empty string
According to the Help files:

----------------------
When doing a string comparison, a null reference is equivalent to the string
literal ""
----------------------

As demonstrated by
----------------------------
Trace.WriteLine("" = Nothing)
----------------------------
which returns true.

as opposed to
-----------------------------
Trace.WriteLine("" is Nothing)
------------------------------

which returns false.

Not that I'd recommend doing it this way if you're comparing a lot of
strings in a loop (for readability as much as performance). Better to use a
combination of "is nothing" and "string.length = 0"

HTH,

Trev.

"One Handed Man [ OHM# ]" <O_H_M{at}BTInternet{dot}com> wrote in message
news:ea**************@TK2MSFTNGP10.phx.gbl... 1.) Nothing wrong with strMessage <> String.Empty,

2.) but lots wrong with strMessage <> nothing if you think you are checking for an empty string

OHM

Erik Frey wrote:
"Cor" <no*@non.com> wrote in message
news:u5**************@TK2MSFTNGP10.phx.gbl...
Hi Kerberoz

In addition to OHT

In this newsgroup we mostly advice to use
strMessage <> ""
or seldom
strMessage <> nothing
but never
strMessage <> String.Empty

Cor


What's wrong with strMessage <> String.Empty?

Just curious.

Erik


--
Best Regards - OHM

O_H_M{at}BTInternet{dot}com

Nov 20 '05 #9

P: n/a
I'm amazed and stand corrected. However, Now I think about it more, it
absolutely makes sense.

Thanks for pointing that out.
Regards - OHM
2.) but lots wrong with strMessage <> nothing
if you think you are checking for an empty string


According to the Help files:

----------------------
When doing a string comparison, a null reference is equivalent to the
string literal ""
----------------------

As demonstrated by
----------------------------
Trace.WriteLine("" = Nothing)
----------------------------
which returns true.

as opposed to
-----------------------------
Trace.WriteLine("" is Nothing)
------------------------------

which returns false.

Not that I'd recommend doing it this way if you're comparing a lot of
strings in a loop (for readability as much as performance). Better to
use a combination of "is nothing" and "string.length = 0"

HTH,

Trev.

"One Handed Man [ OHM# ]" <O_H_M{at}BTInternet{dot}com> wrote in
message news:ea**************@TK2MSFTNGP10.phx.gbl...
1.) Nothing wrong with strMessage <> String.Empty,

2.) but lots wrong with strMessage <> nothing if you think you are
checking for an empty string

OHM

Erik Frey wrote:
"Cor" <no*@non.com> wrote in message
news:u5**************@TK2MSFTNGP10.phx.gbl...
Hi Kerberoz

In addition to OHT

In this newsgroup we mostly advice to use
strMessage <> ""
or seldom
strMessage <> nothing
but never
strMessage <> String.Empty

Cor

What's wrong with strMessage <> String.Empty?

Just curious.

Erik


--
Best Regards - OHM

O_H_M{at}BTInternet{dot}com


--
Best Regards - OHM

O_H_M{at}BTInternet{dot}com
Nov 20 '05 #10

P: n/a
Cor
Hi Trev,

Can you tell where in the helpfiles, because I think this is completly nuts

In my opinion it is this

If string Is nothing means that there is a declared String withouth a
pointer to a virtual memoryadres

If string = nothing
or
if string = string.empty
or
if string = ""
or
if string.length = 0

Means that there is a declared String with a pointer to a virtual memory
adres with a length of zero.

Not that difficult I thought,

Cor
Nov 20 '05 #11

P: n/a
> Can you tell where in the helpfiles,
because I think this is completly nuts
Look under the help for the "<>" or "=" operator. It's under the section
about string comparison. I couldn't find the exact page in MSDN online, but
it is in the help files installed with Visual Studio.
If string Is nothing means that there is a
declared String withouth a
pointer to a virtual memoryadres
I agree, but the implementation of the "=" and "<>" operators for strings
does an extra check and returns True (or false for <>) for "" = Nothing.

Note I'm talking about "=" and not "is". i.e.

"" = Nothing returns true
"" is Nothing returns false

Dim szTest as String

szTest = Nothing returns true
szTest is Nothing returns true

' Assign szTest to an empty string
szTest = ""

szTest = Nothing returns true
szTest is Nothing returns false

I admit, this is slightly confusing, that's why I don't use this method -
it's harder to read.

HTH,

Trev.

"Cor" <no*@non.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl... Hi Trev,

Can you tell where in the helpfiles, because I think this is completly nuts
In my opinion it is this

If string Is nothing means that there is a declared String withouth a
pointer to a virtual memoryadres

If string = nothing
or
if string = string.empty
or
if string = ""
or
if string.length = 0

Means that there is a declared String with a pointer to a virtual memory
adres with a length of zero.

Not that difficult I thought,

Cor

Nov 20 '05 #12

P: n/a

Not really. If you think about it as strings are immutable in .NET, if you
assing a zero length tsring to a string type identifier, then that means
that it points to nowhere on the heap. This equals nothing as a value,
however the string object istself is still on the stack.

Whereas to

str = "" '//does not mean that str Is Nothing. Just its value is nothing.
'See the difference ?

OHM

Cor wrote:
Hi Trev,

Can you tell where in the helpfiles, because I think this is
completly nuts

In my opinion it is this

If string Is nothing means that there is a declared String withouth a
pointer to a virtual memoryadres

If string = nothing
or
if string = string.empty
or
if string = ""
or
if string.length = 0

Means that there is a declared String with a pointer to a virtual
memory adres with a length of zero.

Not that difficult I thought,

Cor


--
Best Regards - OHM

O_H_M{at}BTInternet{dot}com
Nov 20 '05 #13

P: n/a
Cor
Hi Trev,
Note I'm talking about "=" and not "is". i.e.


I was thinking you was talking about IS therefore my message, forget it
than, now I understand what you did want to say.

Cor
Nov 20 '05 #14

P: n/a
Cor
Hi OHM,

I did understand Trev wrong,

I thought that he wanted to say that "Is nothing" is equal to "= nothing"
with a string.

Forget my message.

Cor
Nov 20 '05 #15

P: n/a
Hello, Trev:

The page you mentioned could be http://msdn.microsoft.com/library/de...comparison.asp

You can avoid the confusion with '=Nothing' and 'Is Nothing' if you take into account that String is a class, so a non initialized string variable contains a null reference, not a null string like in previous versions of Basic.
So, use 'Is Nothing' only to check if a string variable has been initialized, for example, before invoking a non shared method, like MyStringVariable.Length; and avoid using =Nothing for strings, as it is equivalent to ="" (or =string.empty) and these are clearer (in my opinion).
I think the better practice for beginners is always initialize string variables in its declaration statement, (dim sv as string="") so there is no problem invoking string members. Don't you think so?

Regards.
"Trev Hunter" <hu*********@hotmail.com> escribió en el mensaje news:u8**************@tk2msftngp13.phx.gbl...
| > Can you tell where in the helpfiles,
| > because I think this is completly nuts
|
| Look under the help for the "<>" or "=" operator. It's under the section
| about string comparison. I couldn't find the exact page in MSDN online, but
| it is in the help files installed with Visual Studio.
|
| > If string Is nothing means that there is a
| > declared String withouth a
| > pointer to a virtual memoryadres
|
| I agree, but the implementation of the "=" and "<>" operators for strings
| does an extra check and returns True (or false for <>) for "" = Nothing.
|
| Note I'm talking about "=" and not "is". i.e.
|
| "" = Nothing returns true
| "" is Nothing returns false
|
| Dim szTest as String
|
| szTest = Nothing returns true
| szTest is Nothing returns true
|
| ' Assign szTest to an empty string
| szTest = ""
|
| szTest = Nothing returns true
| szTest is Nothing returns false
|
| I admit, this is slightly confusing, that's why I don't use this method -
| it's harder to read.
|
| HTH,
|
| Trev.

Nov 20 '05 #16

P: n/a
The page you mentioned could be http://msdn.microsoft.com/library/de...comparison.asp

Nope. I seen that one and it isn't the same as the one in the help files
installed with Visual studio. Go to the Index and type in "<>" to get the
page on the "<> operator". You may need to filter with Visul Basic.
I think the better practice for beginners
is always initialize string variables in its
declaration statement, (dim sv as string="")
so there is no problem invoking string members.
Don't you think so?


Yes, but IMHO, it's also important for beginners to learn the difference
too.

Best Regards,

Trev.

"José Manuel Agüero" <jmaguero_vodafone.es> wrote in message
news:OV*************@TK2MSFTNGP12.phx.gbl...
Hello, Trev:

The page you mentioned could be
http://msdn.microsoft.com/library/de...comparison.asp

You can avoid the confusion with '=Nothing' and 'Is Nothing' if you take
into account that String is a class, so a non initialized string variable
contains a null reference, not a null string like in previous versions of
Basic.
So, use 'Is Nothing' only to check if a string variable has been
initialized, for example, before invoking a non shared method, like
MyStringVariable.Length; and avoid using =Nothing for strings, as it is
equivalent to ="" (or =string.empty) and these are clearer (in my opinion).
I think the better practice for beginners is always initialize string
variables in its declaration statement, (dim sv as string="") so there is no
problem invoking string members. Don't you think so?

Regards.
"Trev Hunter" <hu*********@hotmail.com> escribió en el mensaje
news:u8**************@tk2msftngp13.phx.gbl...
| > Can you tell where in the helpfiles,
| > because I think this is completly nuts
|
| Look under the help for the "<>" or "=" operator. It's under the section
| about string comparison. I couldn't find the exact page in MSDN online,
but
| it is in the help files installed with Visual Studio.
|
| > If string Is nothing means that there is a
| > declared String withouth a
| > pointer to a virtual memoryadres
|
| I agree, but the implementation of the "=" and "<>" operators for strings
| does an extra check and returns True (or false for <>) for "" = Nothing.
|
| Note I'm talking about "=" and not "is". i.e.
|
| "" = Nothing returns true
| "" is Nothing returns false
|
| Dim szTest as String
|
| szTest = Nothing returns true
| szTest is Nothing returns true
|
| ' Assign szTest to an empty string
| szTest = ""
|
| szTest = Nothing returns true
| szTest is Nothing returns false
|
| I admit, this is slightly confusing, that's why I don't use this method -
| it's harder to read.
|
| HTH,
|
| Trev.
Nov 20 '05 #17

This discussion thread is closed

Replies have been disabled for this discussion.