473,385 Members | 2,028 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,385 software developers and data experts.

not or <>?

which is faster?

If strMessage <> String.Empty Then

End If

or

If Not strMessage Is Nothing Then

End If
Nov 20 '05 #1
16 1413
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
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
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
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
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
"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
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
> 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
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
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
> 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

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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Eshrath | last post by:
Hi, What I am trying to do: ======================= I need to form a table in html using the xsl but the table that is formed is quite long and cannot be viewed in our application. So we are...
2
by: Donald Firesmith | last post by:
I am having trouble having Google Adsense code stored in XSL converted properly into HTML. The <> unfortunately become &lt; and &gt; and then no longer work. XSL code is: <script...
1
by: RJN | last post by:
Hi I'm using XMLTextReader to parse the contents of XML. I have issues when the xml content itself has some special characters like & ,> etc. <CompanyName>Johnson & Jhonson</CompanyName>...
1
by: JezB | last post by:
I'm binding a DataGrid web-control to data fetched from a database. However some of my data fields contain text that is within <...> characters - I notice that everything between the <> is...
3
by: | last post by:
I have been researching articles on google on how to create a simple RSS feed that sucks <title><blurb><link><date> out of a sql server 2000 database via an aspx page. I know it has to be pushed...
1
by: RJN | last post by:
Hi I'm using XMLTextReader to parse the contents of XML. I have issues when the xml content itself has some special characters like & ,> etc. <CompanyName>Johnson & Jhonson</CompanyName>...
1
by: mike | last post by:
I've got some code like this: gametype_id = Request.Form("gametype_id") response.write "<br>gametype_id from form>" & gametype_id & "<" response.write "<br>gametype_id from database>" &...
3
by: ajay2552 | last post by:
Hi, I have a query. All html tags start with < and end with >. Suppose i want to display either '<' or '>' or say some text like '<Company>' in html how do i do it? One method is to use &lt,...
14
by: Michael | last post by:
Since the include function is called from within a PHP script, why does the included file have to identify itself as a PHP again by enclosing its code in <?php... <?> One would assume that the...
3
by: Josh Valino | last post by:
Hi, I have a client that has our product and in one of the aspx files, there is code like this: <%= SomePublicProperty %> where the public property returns a string. In the test...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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.