Jay,
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> schrieb:
The use of the String.Empty initial value by MS is also a bit dumb -
aren't
all strings empty by default when they're initialised?
No, all strings are Nothing by default when they're initialized.
That's true. Strings are initialized with 'Nothing', not 'String.Empty'.
In addition to Cor's example. Try the following:
Dim content As String
If content.Length = 0 Then
' Never gets here
' as an NullReferenceException was raised!
End IF
VB considers a String variable that is Nothing and a String variable that
has String.Empty in it as equal.
Just to make sure that this is not misread: The code above won't throw an
exception if 'String.Empty' was assigned to 'content'. If this was the
case, 'content.Length' would return 0.
What VB does is considering a string variable that points to 'Nothing' and a
string variable that has 'String.Empty' in it as equal when comparing these
string variables using '=':
\\\
Dim s1 As String
Dim s2 As String = String.Empty
MsgBox(CStr(s1 = s2)) ' 'True'.
///
.... and...
\\\
Dim s1 As String
Dim s2 As String = String.Empty
MsgBox(CStr(s1 Is s2)) ' 'False'.
///
--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>