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

"" and Nothing

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
Nov 21 '05 #1
31 1764
"" 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

"Xero" <jeff_@_chezjeff_._net(remove_underscores_and_this )> wrote in message
news:53**********************************@microsof t.com...
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
.
.
.
|- (4, 5) | ""
|- (4, 6) | ""

What is the difference between "" and Nothing? Thanks.

--
Xero

http://www.chezjeff.net
My personal web portal

Nov 21 '05 #2
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.
"Greg Burns" <greg_burns@DONT_SPAM_ME_hotmail.com> wrote in message
news:ON**************@tk2msftngp13.phx.gbl...
"" 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

"Xero" <jeff_@_chezjeff_._net(remove_underscores_and_this )> wrote in
message news:53**********************************@microsof t.com...
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
.
.
.
|- (4, 5) | ""
|- (4, 6) | ""

What is the difference between "" and Nothing? Thanks.

--
Xero

http://www.chezjeff.net
My personal web portal


Nov 21 '05 #3
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

"Xero" <jeff_@_chezjeff_._net(remove_underscores_and_this )> wrote in message
news:53**********************************@microsof t.com...
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
.
.
.
|- (4, 5) | ""
|- (4, 6) | ""

What is the difference between "" and Nothing? Thanks.

--
Xero

http://www.chezjeff.net
My personal web portal

Nov 21 '05 #4
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
In VB.NET setting an object variable to Nothing is the same as setting it
to Null.
"Greg Burns" <greg_burns@DONT_SPAM_ME_hotmail.com>

"" 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

"Xero" <jeff_@_chezjeff_._net(remove_underscores_and_this )> wrote in
message news:53**********************************@microsof t.com...
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
.
.
.
|- (4, 5) | ""
|- (4, 6) | ""

What is the difference between "" and Nothing? Thanks.

--
Xero

http://www.chezjeff.net
My personal web portal



Nov 21 '05 #5
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
Nov 21 '05 #6
"Xero" <jeff_@_chezjeff_._net(remove_underscores_and_this )> schrieb:
What is the difference between "" and Nothing? Thanks.


<URL:http://www.google.de/groups?q=dotnet+string.empty+nothing>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #7
> 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

"Cor Ligthert" <no************@planet.nl> wrote in message
news:%2****************@tk2msftngp13.phx.gbl... 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

Nov 21 '05 #8
>> 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


No, in this case, it is String = ""

Nov 21 '05 #9
> 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.
Nov 21 '05 #10
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

"Scott M." <s-***@nospam.nospam> wrote in message
news:ei**************@tk2msftngp13.phx.gbl...
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.

Nov 21 '05 #11
Scott,

"Scott M." <s-***@nospam.nospam> schrieb:
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


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'.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #12
"Scott M." <s-***@nospam.nospam> schrieb:
Setting a Value to Nothing means setting it to its default value (what is
for a string "")

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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #13
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
"Scott M." <s-***@nospam.nospam>
..
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


No, in this case, it is String = ""

Nov 21 '05 #14
Herfried,
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.

???

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
Nov 21 '05 #15
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.

"Greg Burns" <greg_burns@DONT_SPAM_ME_hotmail.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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

"Scott M." <s-***@nospam.nospam> wrote in message
news:ei**************@tk2msftngp13.phx.gbl...
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.


Nov 21 '05 #16

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:%2***************@TK2MSFTNGP15.phx.gbl...
"Scott M." <s-***@nospam.nospam> schrieb:
Setting a Value to Nothing means setting it to its default value (what
is for a string "")

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).

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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #17
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
"Scott M." <s-***@nospam.nospam> wrote in message
news:O6*************@TK2MSFTNGP12.phx.gbl...
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.

"Greg Burns" <greg_burns@DONT_SPAM_ME_hotmail.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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

"Scott M." <s-***@nospam.nospam> wrote in message
news:ei**************@tk2msftngp13.phx.gbl...
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.



Nov 21 '05 #18

"Greg Burns" <greg_burns@DONT_SPAM_ME_hotmail.com> wrote
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


Nov 21 '05 #19

"Scott M." <s-***@nospam.nospam> wrote
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

Nov 21 '05 #20
All you are showing here is the value ASSOCIATED with X, the IL doesn't show
us what is happening in the memory blocks. Looking at this in IL doesn't
show us anything we couldn't see in VB.NET.

We all agree that X will return a zero value after setting it to nothing,
what I'm saying is that when X is accessed after setting it equal to
nothing, the REASON we get zero is because this is a NEW X that hasn't been
initialized yet. The OLD X remains in memory, but is not accessible.
"Larry Serflaten" <se*******@usinternet.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...

"Greg Burns" <greg_burns@DONT_SPAM_ME_hotmail.com> wrote
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

Nov 21 '05 #21
See earlier reply. Looking at this in IL doesn't tell us about the memory
address, only the value.

"Larry Serflaten" <se*******@usinternet.com> wrote in message
news:uP**************@TK2MSFTNGP10.phx.gbl...

"Scott M." <s-***@nospam.nospam> wrote
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

Nov 21 '05 #22

"Scott M." <s-***@nospam.nospam> wrote
See earlier reply. Looking at this in IL doesn't tell us about the memory
address, only the value.
This wasn't IL, it was a section of native code (Assembly) compiled from
the VB commands:

Dim x As Integer
x = 10
x = Nothing
x = 20
x = 0
x = 10
00000010 mov esi,0Ah
Loads 10 into the memory register esi
x = Nothing
00000015 xor esi,esi
Clears the memory register esi
x = 20
00000017 mov esi,14h
Loads 20 into the memory register esi
x = 0
0000001c xor esi,esi


Clears the memory register esi
Earlier reply: All you are showing here is the value ASSOCIATED with X, the IL doesn't show
us what is happening in the memory blocks. Looking at this in IL doesn't
show us anything we couldn't see in VB.NET.

We all agree that X will return a zero value after setting it to nothing,
what I'm saying is that when X is accessed after setting it equal to
nothing, the REASON we get zero is because this is a NEW X that hasn't been
initialized yet. The OLD X remains in memory, but is not accessible.


As shown above, X was assigned four values, including Nothing. There
was no code compiled that indicated anything about the destruction or
creation of anything else in memory. You indicate that after being set to
Nothing, X needs to be re-created to be accessed again. Where in the
above code is that happening? Since the above code in the native language
of the CPU, if it isn't happening there, it just isn't happening....

If we move x out to the module level, the code has to access the 'memory blocks'
there, and similar code is created:

x = 10
0000000d mov dword ptr [esi+000000F0h],0Ah
x = Nothing
00000017 mov dword ptr [esi+000000F0h],0
x = 20
00000021 mov dword ptr [esi+000000F0h],14h
x = 0
0000002b mov dword ptr [esi+000000F0h],0
But again, X = Nothing and X = 0 generate the exact same machine language
code. There is no code generated to destroy or create any new variable
reference. Knowing the New keyword is not used with value types, why
would you think that a new value type needed to be created?

???
LFS
Nov 21 '05 #23
On 2004-12-23, Scott M. <s-***@nospam.nospam> wrote:

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:%2***************@TK2MSFTNGP15.phx.gbl...
"Scott M." <s-***@nospam.nospam> schrieb:
Setting a Value to Nothing means setting it to its default value (what
is for a string "")
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.


No, that's just not so. There's no "resurrection" taking place. And
there is no NEW object. The reference to the old object has been set to
Nothing and the old object itself may or may not have been garbage
collected. The reference represented by 's' now points nowhere valid.

The only thing you're seeing is that VB.Net special-cases the '='
operator for strings, which treats Nothing as somewhat equivalent to
string.empty. This may appear at first sight to resemble a new object
with a default value, but it isn't, as seen by the fact that you can't
access any properties that 's' has after it has been set to Nothing.

Nov 21 '05 #24
Cor,

"Cor Ligthert" <no************@planet.nl> schrieb:
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.

???

Do you really mean this?


Yes, in the special case of the 'String' datatype, which is a reference
type! In this case, assigning 'Nothing' (or "setting the string variable's
value to 'Nothing'") will indeed make the variable containing a null
reference (a reference to 'Nothing') and not contain an empty string.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #25
"David" <df*****@woofix.local.dom> schrieb:
> Setting a Value to Nothing means setting it to its default value (what
> is for a string "")

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.


No, that's just not so. There's no "resurrection" taking place. And
there is no NEW object. The reference to the old object has been set to
Nothing and the old object itself may or may not have been garbage
collected. The reference represented by 's' now points nowhere valid.

The only thing you're seeing is that VB.Net special-cases the '='
operator for strings, which treats Nothing as somewhat equivalent to
string.empty. This may appear at first sight to resemble a new object
with a default value, but it isn't, as seen by the fact that you can't
access any properties that 's' has after it has been set to Nothing.


I couldn't have explained that better! Notice that only the '='
/comparison/ operator has this special behavior, not the '=' assignment
operator, which will truly assign a null reference.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #26
Larry,

"Larry Serflaten" <se*******@usinternet.com> schrieb:
But again, X = Nothing and X = 0 generate the exact same machine language
code. There is no code generated to destroy or create any new variable
reference. Knowing the New keyword is not used with value types, why
would you think that a new value type needed to be created?


'New' is optional for value types, except when calling a value type's
parameterized constructor. So, 'New' is /not/ an indicator for a type being
a value type! In other words, 'New' doesn't say anything about references,
it only says that a new object/value is created.

Where the value is stored and/or if the variable is placed on another
position is an implementation detail which it's not worth to worry about,
except when dealing with unsafe pointers to variables, which is rarely the
case within VB.NET code.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #27
Cor,

"Cor Ligthert" <no************@planet.nl> schrieb:
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.


'Dim s As String' is the same as 'Dim s As String = Nothing', but it's
different from 'Dim s As String = ""', which is the same as 'Dim s As String
= String.Empty'.

In most brains, strings are a value type and VB.NET tries to make value type
semantics available for .NET's 'String' reference type. VB.NET's functions
like 'Len', for example, treat a null reference like a zero-length string.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #28
Very interesting.

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:OH**************@TK2MSFTNGP11.phx.gbl...
"David" <df*****@woofix.local.dom> schrieb:
>> Setting a Value to Nothing means setting it to its default value
>> (what
>> is for a string "")

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.


No, that's just not so. There's no "resurrection" taking place. And
there is no NEW object. The reference to the old object has been set to
Nothing and the old object itself may or may not have been garbage
collected. The reference represented by 's' now points nowhere valid.

The only thing you're seeing is that VB.Net special-cases the '='
operator for strings, which treats Nothing as somewhat equivalent to
string.empty. This may appear at first sight to resemble a new object
with a default value, but it isn't, as seen by the fact that you can't
access any properties that 's' has after it has been set to Nothing.


I couldn't have explained that better! Notice that only the '='
/comparison/ operator has this special behavior, not the '=' assignment
operator, which will truly assign a null reference.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #29
(Inline)

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote
But again, X = Nothing and X = 0 generate the exact same machine language
code. There is no code generated to destroy or create any new variable
reference. Knowing the New keyword is not used with value types, why
would you think that a new value type needed to be created?
'New' is optional for value types, except when calling a value type's
parameterized constructor. So, 'New' is /not/ an indicator for a type being
a value type!


Where did I say it was an indicator? I simply said it wasn't used for value
types. You seem to say I said New was not allowed, but read it again, I
simply said it was not used. Perhaps that would have been better said, 'New
is not commonly used ...' but I didn't think I would need to elaborate!
Where the value is stored and/or if the variable is placed on another
position is an implementation detail which it's not worth to worry about,


What does that mean? Are you trying to say that the machine code does not
indicate how variables are treated? If that is it, I'd have to differ, if it doesn't
happen at the machine code level, then it doesn't get done....

What I showed indicated that there was no code executed that either destroyed
or created a reference, as was indicated by Scott's statement:
X now does NOT equal 0. X IS Nothing, because the object variable (X)
was explicitly destroyed.


What I posted indicated that X=Nothing was identical to X=0, and thus, no
destruction took place.

Investigating further shows that using New on a value type does have an
adverse effect (MSIL and Assmbly):

//000046: Dim a As Integer
//000047: a = Nothing
IL_0001: ldc.i4.0
IL_0002: stloc.0
//000048: a = 0
IL_0003: ldc.i4.0
IL_0004: stloc.0
//000049:
//000050: Dim b As New Integer
IL_0005: ldloca.s b
IL_0007: initobj [mscorlib]System.Int32
//000051: b = Nothing
IL_000d: ldc.i4.0
IL_000e: stloc.1
//000052: b = 0
IL_000f: ldc.i4.0
IL_0010: stloc.1
00000007 xor eax,eax
00000009 mov dword ptr [ebp-10h],eax
0000000c mov dword ptr [ebp-4],ecx
0000000f mov dword ptr [ebp-8],edx
00000012 xor esi,esi
00000014 nop
a = Nothing
00000015 xor esi,esi
a = 0
00000017 xor esi,esi
Dim b As New Integer
00000019 mov dword ptr [ebp-10h],0
b = Nothing
00000020 mov dword ptr [ebp-10h],0
b = 0
00000027 mov dword ptr [ebp-10h],0
Even while using New adds a level of indirection, it still shows that
setting a value type to Nothing is 'exactly the same' as assigning a
value of 0. No one would try to claim that assigning a value of 0
causes the variable to be destroyed, would they?

LFS
Nov 21 '05 #30
Herfried,

In this case was I talking about all value types except the String because
for that was an extra row. The String has in my opinion confusing rules
thinking about constants and variables. Not that that is very important,
because it acts always as a value.

Quoute me complete please as I forever say, now it looks if I wrote
something else and you know what I think about this kind of quoting.

Cor

"Herfried K. Wagner [MVP]" <hi***************@gmx.at>
Cor,

"Cor Ligthert" <no************@planet.nl> schrieb:
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.

???

Do you really mean this?


Yes, in the special case of the 'String' datatype, which is a reference
type! In this case, assigning 'Nothing' (or "setting the string
variable's value to 'Nothing'") will indeed make the variable containing a
null reference (a reference to 'Nothing') and not contain an empty string.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #31
Larry,

"Larry Serflaten" <se*******@usinternet.com> schrieb:
> But again, X = Nothing and X = 0 generate the exact same machine
> language
> code. There is no code generated to destroy or create any new variable
> reference. Knowing the New keyword is not used with value types, why
> would you think that a new value type needed to be created?
'New' is optional for value types, except when calling a value type's
parameterized constructor. So, 'New' is /not/ an indicator for a type
being
a value type!


Where did I say it was an indicator? I simply said it wasn't used for
value
types. You seem to say I said New was not allowed, but read it again, I
simply said it was not used. Perhaps that would have been better said,
'New
is not commonly used ...' but I didn't think I would need to elaborate!


I didn't want to criticise what you say! I just thought that your last
sentence kept something open, which I described.
Where the value is stored and/or if the variable is placed on another
position is an implementation detail which it's not worth to worry about,


What does that mean? Are you trying to say that the machine code does not
indicate how variables are treated?


No, I don't mean that. What I wanted to say is that it's completely
transparent to the programmer where the variable's content is stored. It
can be either stored in-place or a pointer pointing to it. In case of the
value types, it's stored on a certain position and overwritten.
What I showed indicated that there was no code executed that either
destroyed
or created a reference, as was indicated by Scott's statement:
.... and I agree on that.
Investigating further shows that using New on a value type does have an
adverse effect (MSIL and Assmbly):

//000046: Dim a As Integer
//000047: a = Nothing
IL_0001: ldc.i4.0
IL_0002: stloc.0
//000048: a = 0
IL_0003: ldc.i4.0
IL_0004: stloc.0
//000049:
//000050: Dim b As New Integer
IL_0005: ldloca.s b
IL_0007: initobj [mscorlib]System.Int32
//000051: b = Nothing
IL_000d: ldc.i4.0
IL_000e: stloc.1
//000052: b = 0
IL_000f: ldc.i4.0
IL_0010: stloc.1
00000007 xor eax,eax
00000009 mov dword ptr [ebp-10h],eax
0000000c mov dword ptr [ebp-4],ecx
0000000f mov dword ptr [ebp-8],edx
00000012 xor esi,esi
00000014 nop
a = Nothing
00000015 xor esi,esi
a = 0
00000017 xor esi,esi
Dim b As New Integer
00000019 mov dword ptr [ebp-10h],0
b = Nothing
00000020 mov dword ptr [ebp-10h],0
b = 0
00000027 mov dword ptr [ebp-10h],0
Even while using New adds a level of indirection, it still shows that
setting a value type to Nothing is 'exactly the same' as assigning a
value of 0. No one would try to claim that assigning a value of 0
causes the variable to be destroyed, would they?


Full ACK.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #32

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

Similar topics

1
by: Pete Mahoney | last post by:
Ok I use a textarea to store data input by the user, and then upon them clicking the submit button I store this data to a database. The problem is once the user inputs too much data (about 3...
0
by: Michael | last post by:
I have a problem forcing files to download. If I select Save the document is saved with no problems. If I select "Open" the document is empty or I get a "File not found" error from the application...
13
by: Don Vaillancourt | last post by:
What's going on with Javascript. At the beginning there was the "undefined" value which represented an object which really didn't exist then came the null keyword. But yesterday I stumbled...
3
by: Phil Powell | last post by:
'GET THE HTML CONTENT FOR DISPLAY BAND ORIGIN Dim bandOriginDropdown On Error Resume Next set scraper = Server.CreateObject("Microsoft.XMLHTTP") if err then bandOriginDropdown = "" else...
13
by: MLH | last post by:
I have a textbox control used to enter a single alphanumeric character. It changes the case of "i" to upper case. But it does not do so with the letter "g". Its left as "g" - not changed to "G"? ...
9
by: Dan | last post by:
I am trying to use Request.Form("__EVENTTARGET") to get the name of the control that caused a post back. It keeps returning "". I am not really sure why, this happens for all of my controls...
4
by: Guoqi Zheng | last post by:
On my application, I need to have different action based on the pass in query string. When the query string is not presented, I try to use If request.querystring("id") ="" THEN ...... This is...
6
by: Rob Meade | last post by:
Hi all, Ok - I've used the word gramatically - which I'm sure is incorect, however I mean when VS2005 lists "warnings" like this one: Warning 1 Variable 'Command' is used before it has been...
14
by: Atara | last post by:
I know in C++ it is true. but what about VB .Net and its GarbageCollector ? For example, consider the following case. Does f2() needs to do any disposing code ? Public Sub f1() As...
21
by: comp.lang.tcl | last post by:
set php {<? print_r("Hello World"); ?>} puts $php; # PRINTS OUT <? print_r("Hello World"); ?> puts When I try this within TCL I get the following error:
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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:
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
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...

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.