not or <>?

  • Thread starter Thread starter kerberoz
  • Start date Start date
K

kerberoz

which is faster?

If strMessage <> String.Empty Then

End If

or

If Not strMessage Is Nothing Then

End If
 
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
 
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
 
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
 
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,
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
 
Cor said:
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
 
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
 
2.) but lots wrong with strMessage said:
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.
 
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
 
I'm amazed and stand corrected. However, Now I think about it more, it
absolutely makes sense.

Thanks for pointing that out.


Regards - OHM
 
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.
 
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
 
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
 
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
 
Hello, Trev:

The page you mentioned could be http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vblr7/html/vagrpcomparison.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" <[email protected]> escribió en el mensaje | > 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.
 
The page you mentioned could be
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vblr7/html/vagrpcomparison.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 said:
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
Hello, Trev:

The page you mentioned could be
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vblr7/html/vagrpcomparison.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" <[email protected]> escribió en el mensaje
| > 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.
 
Back
Top