"" and Nothing

G

Guest

I have an array in my program. (Declared as letters(16, 16)).
When I paused the program in debugging mode, I saw the following in the
Autos window:

letters
|- (0, 0) | Nothing
|- (0, 1) | Nothing
 
G

Greg Burns

"" is a zero-length string. String.Empty will return this value.

Nothing is the value of a string prior to assignment.

Dim s1 as string <-- s1 has a value of Nothing
Dim s2 as string = String.Empty

debug.writeline(s1.Length) <-- will cause an null reference exception!!!
debug.writeline(s2.Length) <-- prints 0

Greg
 
S

Scott M.

Think of it this way...

When you declare a variable, but do NOT initialize it, the variable points
to an allocated memory address that does not yet have anything in it, so the
variable points to "Nothing".

When you have a string variable that is or was initialized, but then has
it's value set to "", the String object does exist in memory and the
variable does point to that string (so it's NOT nothing), but the string is
zero length or "".

In VB.NET setting an object variable to Nothing is the same as setting it to
Null.
 
G

Gerald Hernandez

To over-simplify, but get the idea across...
When you declare a variable and don't initialize it, as in:
Dim s1 As String 's1 is just a placeholder to hold a String at some point

It is like clearing a place on your desk to hold a piece of paper.
There is not actually a piece of paper on your desk, so there is Nothing.

"" means an empty string. This is like a blank piece of paper. The paper is
there, so it is definately not Nothing, but there is nothing on the paper,
so it is Empty.
You can declare and initialize a variable at the same time, as in:
Dim s2 As String = String.Empty 's2 is a string, but it is empty, or ""

Gerald
 
C

Cor Ligthert

Hi,

Just a little thing to add to Scotts answer trying to make IS and = clear,
because for the rest I have nothing to add.
When you declare a variable, but do NOT initialize it, the variable points
to an allocated memory address that does not yet have anything in it, so
the variable points to "Nothing".

In this case it is String Is Nothing
When you have a string variable that is or was initialized, but then has
it's value set to "", the String object does exist in memory and the
variable does point to that string (so it's NOT nothing), but the string
is zero length or "".

In this case it is String = Nothing
 
C

Cor Ligthert

Oh,

I forget this one
In VB.NET setting an object variable to Nothing is the same as setting it
to Null.
Setting a Value to Nothing means setting it to its default value (what is
for a string "")

Cor
 
G

Greg Burns

Setting a Value to Nothing means setting it to its default value (what is
for a string "")

Maybe that explains....

Dim s as string
If s = String.Empty Then...
' this evaluates true


I was expecting another null reference exception like in

Dim s a string
if s.length = 0 then ...
' exception!

Just when you think you understand something... :^)

Greg
 
S

Scott M.

Setting a Value to Nothing means setting it to its default value (what is
for a string "")

No it doesn't. Setting a value to Nothing destroys the object variable (the
pointer) and so the object variable has "no value" (Null in many languages)
or "Nothing".

For example, the default value of any numeric data type is zero. If I
write:

Dim X as Integer
X = 15
X = Nothing

X now does NOT equal 0. X IS Nothing, because the object variable (X) was
explicitly destroyed. The Integer object X was pointing to still exists in
memory (and is still holding a value of 15), but unless there is another
pointer pointing to it, it is unreachable and eligable for Garbage
Collection.
 
G

Greg Burns

Dim X As Integer
X = 15
X = Nothing

If X = 0 Then
MsgBox("true")
End If

;)

Same goes here:

Dim X as Integer

If X=Nothing Then ... 'evaluates true

If X=0 Then... 'also evaluates true

I assume this has to do with the differences between Value Types and
Reference Types.

Greg
 
H

Herfried K. Wagner [MVP]

Scott,

Scott M. said:
No, in this case, it is String = ""

Both of you are right.

\\\
Dim s As String = ""
MsgBox(CStr(s = Nothing)) ' 'True'.
MsgBox(CStr(s Is Nothing)) ' 'False'.
///

In the first case, value equality is checked using the '=' operator. The
default /value/ of a string variable is (implicitly) the empty string
('String.Empty', zero-length string), even if the reference is pointing to
'Nothing'.
 
H

Herfried K. Wagner [MVP]

That's indeed not true.

\\\
s = Nothing
///

.... will assign a null-reference to the string variable, not its default
value "". Nevertheless, the string's default value when it's treated as
sort of value type is "", even if the reference points to 'Nothing'.
No it doesn't. Setting a value to Nothing destroys the object variable
(the pointer) and so the object variable has "no value" (Null in many
languages) or "Nothing".
ACK.

For example, the default value of any numeric data type is zero. If I
write:

Dim X as Integer
X = 15
X = Nothing

X now does NOT equal 0.

'X' equals 0! 'Integer' is a value type, the variable is simply 32 bits!
X IS Nothing, because the object variable (X) was explicitly destroyed.

There is no "object variable" if the integer is not boxed.
The Integer object X was pointing to still exists in memory (and is still
holding a value of 15)

Definitely no. The 32 bits if 'X' are set to 0 when assigning 'Integer''s
default value (which is 0).
pointer pointing to it, it is unreachable and eligable for Garbage
Collection.

Remember that 'Integer' is a value type! If 'X' is a local variable, it's
put onto the stack and the GC does not take care of it because it's removed
automatically when the method call returns.
 
C

Cor Ligthert

Scott,

I showed it in relation to your text and there it is realy
xx as String = nothing, default which mean default value.

That that is for a string "" makes that this mostly is done with "" which I
find nicer by the way.

Cor
 
C

Cor Ligthert

Herfried,
???

Do you really mean this?
A value is not on the managed heap, what object variable are you talking
about.
(Where for the string are 2 posibilities)

Cor
 
S

Scott M.

What you are showing here is an example of object resurrection. The X that
you are testing with (If X = 0...) does not point to the same value in
memory earlier. You are, in effect, creating a new X. But because you
didn't initialize it, it gets the type's default value (zero). In other
words, the X you are referring to before X=Nothing and the X you are
referring to after X=Nothing are 2 different X's.
 
S

Scott M.

Herfried K. Wagner said:
That's indeed not true.

\\\
s = Nothing
///

... will assign a null-reference to the string variable, not its default
value "". Nevertheless, the string's default value when it's treated as
sort of value type is "", even if the reference points to 'Nothing'.

The reason you get back "" is that after setting the string to Nothing and
then accessing it again, the object variable is not able to access the SAME
object in memory that was available before the object was set to Nothing.
Ressurection occurs and the NEW object variable, never having been
initialized, takes on the type's default value. The ORIGINAL object is not
affected. Thus, the X=15 (that I mentioned earlier) is correct, when you
test X for zero after setting it to nothing, you are actually looking at a
different X, or I should say X is pointing at a different object in memory
(one that hasn't been initialized yet and takes on the default value of the
type).
 
G

Greg Burns

This is getting over my head fast. :^)

I do know that Integers are Value Types and do not get Garbage Collected.

An integer variable resides on the stack. I take it that means setting it
to Nothing simply removes it from the stack? Or does it really, just reset
it back to its default of 0 as was implied previously?

I dunno.

Greg
 
L

Larry Serflaten

Greg Burns said:
This is getting over my head fast. :^)

I do know that Integers are Value Types and do not get Garbage Collected.

An integer variable resides on the stack. I take it that means setting it
to Nothing simply removes it from the stack? Or does it really, just reset
it back to its default of 0 as was implied previously?

I dunno.

A quick look at the MSIL shows it simply sets the value back to 0:

HTH
LFS

//000152: Dim x As Integer = 10
IL_0001: ldc.i4.s 10
IL_0003: stloc.0
//000153: x = 20
IL_0004: ldc.i4.s 20
IL_0006: stloc.0
//000154: x = Nothing
IL_0007: ldc.i4.0
IL_0008: stloc.0
//000155: x = 30
IL_0009: ldc.i4.s 30
IL_000b: stloc.0
//000156: x = 0
IL_000c: ldc.i4.0
IL_000d: stloc.0
 
L

Larry Serflaten

Scott M. said:
For example, the default value of any numeric data type is zero. If I
write:

Dim X as Integer
X = 15
X = Nothing

X now does NOT equal 0. X IS Nothing,


That is not how it looks at the lower levels:

x = 10
00000010 mov esi,0Ah
x = Nothing
00000015 xor esi,esi
x = 20
00000017 mov esi,14h
x = 0
0000001c xor esi,esi


Using X = Nothing and X = 0 result in the exact same machine
language code. For the value types they are identical. For the
String type similar code is used as shown for X = Nothing, but
X = "" is more like X = "anything" where both string constants
are located somewhere else in memory (one just does not have
any characters).

HTH
LFS
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top