473,396 Members | 1,938 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,396 software developers and data experts.

String Is Nothing versus String = Nothing

do these mean the same thing ?

Dim s As String
.....

If s Is Nothing Then
.....

If s = Nothing Then
.....
Nov 20 '05 #1
5 2051

Yes and no....

-----------------------------------
Dim s as String

Debug.WriteLine(s is Nothing) ' True
Debug.WriteLine(s = Nothing) ' True
Debug.WriteLine(s = "") ' True

s = "" ' Same as s = String.Empty

Debug.WriteLine(s is Nothing) ' False
Debug.WriteLine(s = Nothing) ' True

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

"s = Nothing" tests to see if it is not assigned to anything OR is equal to
""
"s is nothing" only tests to see if it is not assigned to anything

i.e.
--------------------------

If s = nothing then
.....

' is equlvelent to

If s is nothing orelse s = String.Empty then
....
--------------------------

If you want short code to test for empty strings, use

If s = "" then

but beware of performance if doing this in a big loop because of string
comparisons. The following may have a slight performance advantage:

If s is nothing orelse s.Length = 0 then
HTH,

Trev.

"John A Grandy" <johnagrandy-at-yahoo-dot-com> wrote in message
news:%2******************@TK2MSFTNGP12.phx.gbl...
do these mean the same thing ?

Dim s As String
....

If s Is Nothing Then
....

If s = Nothing Then
....

Nov 20 '05 #2
<<<
The following may have a slight performance advantage:

If s is nothing orelse s.Length = 0 then

why not use

If s = Nothing Then
"Trev Hunter" <hu*********@hotmail.com> wrote in message
news:Ot**************@TK2MSFTNGP12.phx.gbl...
Yes and no....

-----------------------------------
Dim s as String

Debug.WriteLine(s is Nothing) ' True
Debug.WriteLine(s = Nothing) ' True
Debug.WriteLine(s = "") ' True

s = "" ' Same as s = String.Empty

Debug.WriteLine(s is Nothing) ' False
Debug.WriteLine(s = Nothing) ' True

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

"s = Nothing" tests to see if it is not assigned to anything OR is equal to ""
"s is nothing" only tests to see if it is not assigned to anything

i.e.
--------------------------

If s = nothing then
....

' is equlvelent to

If s is nothing orelse s = String.Empty then
...
--------------------------

If you want short code to test for empty strings, use

If s = "" then

but beware of performance if doing this in a big loop because of string
comparisons. The following may have a slight performance advantage:

If s is nothing orelse s.Length = 0 then
HTH,

Trev.

"John A Grandy" <johnagrandy-at-yahoo-dot-com> wrote in message
news:%2******************@TK2MSFTNGP12.phx.gbl...
do these mean the same thing ?

Dim s As String
....

If s Is Nothing Then
....

If s = Nothing Then
....


Nov 20 '05 #3
Cor
Hi John,

No
Try this
\\\
Dim s As String
If s Is Nothing Then MessageBox.Show("IsNothing")
If s = Nothing Then MessageBox.Show("=Nothing")
s = ""
If s Is Nothing Then MessageBox.Show("IsNothing2")
'Isnothing 2 Will not be showed
If s = Nothing Then MessageBox.Show("=Nothing2")
'=Nothing is true
//
I hope this helps?

Cor
Dim s As String
If s Is Nothing Then
If s = Nothing Then

Nov 20 '05 #4
> why not use
If s = Nothing Then
Because if s is not nothing, then a string comparison is done to check if it
is equal to "" wich is slower than checking the length.

If s = Nothing Then

is the same as

If s is Nothing orelse s = "" Then

where as if you use

If s is Nothing orelse s.Length=0 Then

you cut out the s = "" string comparison.

The performance advantage is really small though and will unlikley make any
difference in a real world application (on my machine it averages out about
1/50 sec for 200000 iterations).

HTH,

Trev.
"John A Grandy" <johnagrandy-at-yahoo-dot-com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl... <<<
The following may have a slight performance advantage:

If s is nothing orelse s.Length = 0 then


why not use

If s = Nothing Then
"Trev Hunter" <hu*********@hotmail.com> wrote in message
news:Ot**************@TK2MSFTNGP12.phx.gbl...

Yes and no....

-----------------------------------
Dim s as String

Debug.WriteLine(s is Nothing) ' True
Debug.WriteLine(s = Nothing) ' True
Debug.WriteLine(s = "") ' True

s = "" ' Same as s = String.Empty

Debug.WriteLine(s is Nothing) ' False
Debug.WriteLine(s = Nothing) ' True

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

"s = Nothing" tests to see if it is not assigned to anything OR is equal

to
""
"s is nothing" only tests to see if it is not assigned to anything

i.e.
--------------------------

If s = nothing then
....

' is equlvelent to

If s is nothing orelse s = String.Empty then
...
--------------------------

If you want short code to test for empty strings, use

If s = "" then

but beware of performance if doing this in a big loop because of string
comparisons. The following may have a slight performance advantage:

If s is nothing orelse s.Length = 0 then
HTH,

Trev.

"John A Grandy" <johnagrandy-at-yahoo-dot-com> wrote in message
news:%2******************@TK2MSFTNGP12.phx.gbl...
do these mean the same thing ?

Dim s As String
....

If s Is Nothing Then
....

If s = Nothing Then
....



Nov 20 '05 #5
* "John A Grandy" <johnagrandy-at-yahoo-dot-com> scripsit:
do these mean the same thing ?

Dim s As String
....

If s Is Nothing Then
....

If s = Nothing Then
....


In addition to the other replies -- you can query old messages of this
group using Google Groups Search (<http://www.deja.com>). Just select
this group (microsoft.public.dotnet.languages.vb) in the advanced groups
search and "feed" Google with the keywords. I remember there were some
threads about this topic in the past.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #6

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

Similar topics

108
by: Bryan Olson | last post by:
The Python slice type has one method 'indices', and reportedly: This method takes a single integer argument /length/ and computes information about the extended slice that the slice object would...
18
by: Steve Litvack | last post by:
Hello, I have built an XMLDocument object instance and I get the following string when I examine the InnerXml property: <?xml version=\"1.0\"?><ROOT><UserData UserID=\"2282\"><Tag1...
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
33
by: genc_ymeri | last post by:
Hi over there, Propably this subject is discussed over and over several times. I did google it too but I was a little bit surprised what I read on internet when it comes 'when to use what'. Most...
87
by: Robert Seacord | last post by:
The SEI has published CMU/SEI-2006-TR-006 "Specifications for Managed Strings" and released a "proof-of-concept" implementation of the managed string library. The specification, source code for...
8
by: ronrsr | last post by:
I have a single long string - I'd like to split it into a list of unique keywords. Sadly, the database wasn't designed to do this, so I must do this in Python - I'm having some trouble using the...
1
by: DLN | last post by:
Hello all, I have a quick question regarding how best to use static strings in my C# code that I'm hoping someone can help me with. Is there any advantage/disadvantage from a performance...
21
by: kumarvis | last post by:
int main () { char *str = *Aamit ; *str='R' ; printf("%s \n " ,str); } it's giving segfault why ?????
5
by: Ray | last post by:
Hi all, I am thinking of subclassing the standard string class so I can do something like: mystring str; .... str.toLower (); A quick search on this newsgroup has found messages by others
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.