empty vs null

  • Thread starter Thread starter juzan
  • Start date Start date
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.
 
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
 
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.
 
Back
Top