Stephany Young <noone@localhost> wrote:
> Oooooh, Oooooh, I do!
>
> What you are seeing is the VB.Net equivalent of an optical illusion. To
> demonstrate -
>
> Try this:
>
> Dim _string1 As String
>
> Console.WriteLine(_string1 Is Nothing)
>
> Console.WriteLine(_string1 = "")
>
> Console.WriteLine(_string1.Length = 0)
>
> The results should be:
>
> True
> True
> Exception (Object reference not set to an instance of an object)
>
> "But,", I hear you ask, "if the 3rd Console.WriteLine threw an exception
> then why didn't the 2nd Console.WriteLine throw an exception also?".
That's because VB.NET has overloaded the = operator for strings, and
when comparing strings, considers null (Nothing) and the empty string
as being the same. (It doesn't strike me as a good thing, but there we
go.) Here's part of the specs (from section 11.14, Relational
Operators):
<quote>
String. The operators return the result of comparing the two values
using either a binary comparison or a text comparison. The comparison
used is determined by the compilation environment and the Option
Compare statement. A binary comparison determines whether the numeric
Unicode value of each character in each string is the same. A text
comparison does a Unicode text comparison based on the current culture
in use on the .NET Framework. When doing a string comparison, a null
reference is equivalent to the string literal "".
</quote>
> Good question. I don't fully understand why, but if you 'query' the value of
> an uninitialised variable whose Type is a 'Value Type' then the value
> returned is default value for that Type, but the variable itself stays
> uninitialised.
Note that string isn't a value type, by the way.
<snip>
> Try this:
>
> Dim _string1 As String = "abc" & ControlChars.NullChar & "def"
>
>
> Console.WriteLine(_string1.Length)
>
> Console.WriteLine(_string1)
>
> While one might expect to see the results displayed as
>
> 6
> abcdef
>
> The actual results will be:
>
> 7
> abc
>
> Note that everything after the NUL character has been suppressed.
Not on my box. On mine I get:
7
abc def
(which is what I'd expect).
There used to be a bug in the debugger which would terminate strings on
null characters, and certainly various GUI elements assume null
termination, but the console doesn't.
--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too