Re: empty vs null

From: Bill McCarthy (no)
Date: 03/31/04


Date: Wed, 31 Mar 2004 22:35:52 +1000

Hi Matthew,

The documentation you refer to is for the System.String type, not for VB's
equality operator.
>From VB, you can use VB's operators fro intrinsic types, or you can use the
methods of the underlying framework type such as String.Equals and
MyString.Op_Equality etc. So that documentation is actually also correct, just
it is talking about the methods the String class has, not VB's intrinsic type
operators.

As to how I came to know this, well that's a long story, no doubt <g> It was
years ago literally, as part of my exploration of the new framework and looking
at how well VB ported to VB. Strings was an interesting change compared to VB6,
yet they did manage to keep the look and feel of Vb strings pretty much, even
though they moved to being immutable reference types. So as we moved from VB6
to Vb.NET, suddenly we could actually compare string references, and at the same
time, we could still treat null references like instances. In Vb6, there was
little way to tell if a string was actually a nullstring, except for using
VarPtr. Anyway the differences, and the poor documentation in the early days
(and still somewhat lacking documentation today <g>) drove me to dig down under
the hood to see what was what. You know, that curiosity and the cat thing ;)

Bill.

"Matthew" <matthew_wills@mlc.com.au> wrote in message
news:d9a194a3.0403301729.7e244f24@posting.google.com...
> Bill,
>
> Re:
>
> > VB.NET simplifies a lot of this, by allowing you to check for both
conditions
> > using the equality operator. So code like If MyString = Nothing, checks for
> > both empty and null string conditions.
>
> My docs (VB.NET 1.0) state:
>
> <quote>
> By definition, any String, including the empty string (""), compares
> greater than a null reference (Nothing in Visual Basic); and two null
> references compare equal to each other.
>
> Use the Compare and Equals methods to perform combined reference and
> value comparisons of Object and String instances. The equality and
> inequality relational operators are implemented with the Equals method
> and can also be used to make reference and value comparisons.
> </quote>
>
> which seems to contradict your point above (though feel free to point
> out my error in understanding if you believe it is consistent with
> your point). But when I TRY what you say, you appear to be right...
>
> So, my question is, how did you find your above statement to be true?
> Trial and error? Some other documentation elsewhere?
>
> Thanks
> Matthew
>
> "Bill McCarthy <no spam>" <bill_mcc &#64; iprimus.com.au> wrote in message
news:<O9R#KBUFEHA.3404@TK2MSFTNGP10.phx.gbl>...
> > Hi Juzan,
> >
> > An empty string is "" where a null string is a variable that is yet to be
> > allocated.
> > To understand this, you really need to understand reference types, which is
what
> > a string is. A reference type, is a type when the actual location is pointed
to.
> > So say you have a variable MyString. That variable will be pushed onto the
> > stack when you are working with it. The value of that will be a pointer to
the
> > string type itself, which will reside on the heap. IF the string is empty,
"",
> > there will be a pointer to a location on the heap where there is the empty
> > string "" (or zero length string) If the string is null, then there will
be a
> > zero pointer, that is, no location on the heap.
> >
> > VB.NET simplifies a lot of this, by allowing you to check for both
conditions
> > using the equality operator. So code like If MyString = Nothing, checks for
> > both empty and null string conditions.
> > If you want to check for the reference actually being null, you use the Is
> > operator. So If MyString Is Nothing checks for the null case only.
> >
> >
> > Bill.
> >
> >
> > "juzan" <chris@hotmail.com> wrote in message
> > news:ux9ALpTFEHA.2560@TK2MSFTNGP12.phx.gbl...
> > > hi
> > > what is the difference between an empty string and a null string?
> > > thanx
> > >
> > >



Relevant Pages

  • Re: newbie trouble making array of instances
    ... A new string instance was created by the Removemethod, and the reference to this new instance was assigned to str1. ... that's simply the documentation describing what it does. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Selecting Proper Variables
    ... you was meaning "it can be" a null reference. ... If you compare a string that is ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Selecting Proper Variables
    ... > Although = Nothing acts the same as Is nothing when there is no reference. ... Dim s1 As String = String.Empty ... If you compare a string that is ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Repost: Can anyone help with this Regex problem?
    ... The following sites provide a wealth of information on regular expressions. ... A tutorial & reference on using regular expressions: ... The MSDN's documentation on regular expressions: ... Specifically, the string ...
    (microsoft.public.dotnet.languages.vb)
  • Re: formula validation
    ... I need to be able to reference the formula ... in the cell and compare it to a string containing the formula. ...
    (microsoft.public.excel.programming)