What is Nothing?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I need to understand some basic definitions for things like null, dbnull, and
nothing
Is setting a string variable to an empty string "", the same as setting it
equal to nothing. If there is some published ms document that discusses
"nothing' in detail, I would sure be interested in reading it.

Thanks,
Fred Herring
 
Fred Herring said:
I need to understand some basic definitions for things like null, dbnull,
and
nothing
Is setting a string variable to an empty string "", the same as setting it
equal to nothing. If there is some published ms document that discusses
"nothing' in detail, I would sure be interested in reading it.

Nothing is what a reference variable is set to when it doesn't reference
anything. The empty string (String.Empty, or "") is a reference to a string
object with zero length, so it is clearly different from not referencing
anything at all.

DBNull is a relational database concept, and means something like "no
value". It can mean no value was ever specified, or something has never been
filled in, or it can mean no corresponding row on the other side of an outer
join, but for you it means that there's no value for you to process.

In particular, DBNull does not mean "blank" or zero. Those are values, and
DBNull means that there is no value.

I hope that helps.
John Saunders
 
Thanks for the quick reply to my nothing question. I would like to follow up
the thread a little if I may.

If I dimension a string variable, that variable contains nothing upon
initialization. If I set it equal to "" or string.empty, I have now changed
its value. Testing the variable for nothing at that point would fail. I
would have to specifically test it for string.empty to see if anything
resided in the variable. I guess strings are the confusing data type.
Numeric variables are either nothing or some value (0,1,2...), never blank.

I use sql server 2k. Is a null value in sql the same a nothing in net?

Fred
 
Fred Herring said:
Thanks for the quick reply to my nothing question. I would like to follow
up
the thread a little if I may.

If I dimension a string variable, that variable contains nothing upon
initialization. If I set it equal to "" or string.empty, I have now
changed
its value. Testing the variable for nothing at that point would fail. I
would have to specifically test it for string.empty to see if anything
resided in the variable.

Your terminology is misleading you. When you set it to String.Empty, you
have set it to something - an empty string.

A zero-length string is something - it's a string which has no length. You
can reference the properties of an empty string, in particular, you can say:

Dim es As String = String.Empty
Dim i As Integer = es.Length ' i is now zero

but you can't do this:

Dim es As String = Nothing
Dim i As Integer = es.Length ' Throws a NullReferenceException since es
' Does not reference an
instance of the String type

because Nothing is not something.

' I guess strings are the confusing data type.
Numeric variables are either nothing or some value (0,1,2...), never
blank.

Zero is a value. String.Empty is a reference to a String object with zero
length. Nothing is not a reference to anything.
I use sql server 2k. Is a null value in sql the same a nothing in net?

No. NULL in SQL is DBNull in .NET.

John Saunders
 
I think that John missed one important point.

While it is true that NULL in SQL is DBNull in .NET, and that DBNull is
an object, while null is a reference to nothing (and thus not an
object), it is _also_ true that DBNull and null in .NET arguably
represent the same concept: "There is nothing there."

Up until a few minutes ago I had been confounded by why the .NET
Framework designers decided that they needed a special class called
DBNull to represent something for which they already had a value: null.
In thinking about this post it came to me.

DBNull and null represent a fine distinction: that between "I did read
something from the database, and it says that this column in this row
contains nothing. NULL." and "There is nothing at this location. In the
context of my program, it is uninitialized." DBNull is a marker that
there is nothing at a certain location in the database; null is nothing
at all. In most programs the distinction is irrelevant. In fact, I
have methods coded into my applications that have no purpose other than
to mediate between DBNull and null: a DBNull read from the database is
transformed to a null in memory, and vice versa. I wonder if the fine
distinction between the two isn't more trouble than it's worth.

However, as John points out, null and an empty string are completely
different. That is the distinction between "I have not filled this in,"
and "I filled this in and chose to leave it blank."
 
Bruce Wood said:
I think that John missed one important point.

While it is true that NULL in SQL is DBNull in .NET, and that DBNull is
an object, while null is a reference to nothing (and thus not an
object), it is _also_ true that DBNull and null in .NET arguably
represent the same concept: "There is nothing there."

Up until a few minutes ago I had been confounded by why the .NET
Framework designers decided that they needed a special class called
DBNull to represent something for which they already had a value: null.
In thinking about this post it came to me.

DBNull and null represent a fine distinction: that between "I did read
something from the database, and it says that this column in this row
contains nothing. NULL." and "There is nothing at this location. In the
context of my program, it is uninitialized." DBNull is a marker that
there is nothing at a certain location in the database; null is nothing
at all. In most programs the distinction is irrelevant. In fact, I
have methods coded into my applications that have no purpose other than
to mediate between DBNull and null: a DBNull read from the database is
transformed to a null in memory, and vice versa. I wonder if the fine
distinction between the two isn't more trouble than it's worth.

Bruce,

We're at risk of confusing the OP with subtleties which go beyond the
initial question. However, there are some response I want to make.

First, null (or Nothing) in .NET is what a reference type contains when it
doesn't reference anything. It cannot be used for value types. On the other
hand, an Integer database column could return NULL (DBNull.Value) indicating
that there is no value there, integer or otherwise.

To get more specific about SQL and NULL, consider the following query:

SELECT L.ID, L.A, R.B
FROM LeftTable L LEFT OUTER JOIN RightTable R
ON L.ID = R.LID

Such a query can return things like:

L.ID L.A R.B
1 2 3
2 3 4
3 4 NULL

In this case, the NULL simply means that there is no row in table RightTable
which has LID set to 3, so there is no answer to the question "what is the
value of R.B for this row of the query". This doesn't mean that there is
some row in RightTable that has a NULL in the B column, it means that this
query has no answer to "R.B" in this row.

Recalling what I learned in college about Relational Database Theory, if a
NULLable column of a table has no value, that is exactly equivalent to the
LEFT OUTER JOIN situation, as the theory isn't about tables at all, but is
about tuples, and the tuples you get from a table could as easily have been
generated from some join, and the mathematics still work.

John Saunders

P.S. Past this point, you need an expert in relational database theory,
which I am not.
 
First, null (or Nothing) in .NET is what a reference type contains when it
doesn't reference anything. It cannot be used for value types.

<snip>

Would that life were that simple...

null in C# is indeed only available for reference types.

Nothing in VB.NET is "the default value for the type" - in other words,
null, 0, '\0' or false.

Furthermore, VB.NET treats Nothing in an odd way when it comes to
strings. Len(x) is 0 when x is Nothing, but x.Length will throw an
exception. Also, ""=Nothing is true, despite "".Equals(Nothing) being
false. Presumably this is for backwards compatibility reasons. Blech,
is all I can say...
 
Jon Skeet said:
<snip>

Would that life were that simple...

null in C# is indeed only available for reference types.

Nothing in VB.NET is "the default value for the type" - in other words,
null, 0, '\0' or false.

Furthermore, VB.NET treats Nothing in an odd way when it comes to
strings. Len(x) is 0 when x is Nothing, but x.Length will throw an
exception. Also, ""=Nothing is true, despite "".Equals(Nothing) being
false. Presumably this is for backwards compatibility reasons. Blech,
is all I can say...

Are all of the above true with Options Strict On?

John Saunders
 
Are all of the above true with Options Strict On?

Yup:

Option Strict On

Imports Microsoft.VisualBasic
Imports System

Public Class Test

Public Shared Sub Main()

Dim x as string = nothing
Dim y as string = "x"
Console.WriteLine(Len(x))
Console.WriteLine(""=Nothing)
Console.WriteLine(y.Equals(x))
End Sub

End Class

Output:
0
True
False
 
Ravichandran J.V. said:
Nothing readies an object for collection.

No, it does not. This is not VB6! Consider the following:

Dim a As New MyClass()
Dim b As MyClass = a
a = Nothing

b still has a reference to the MyClass instance, which is not ready for
garbage collection.

John Saunders
 
Back
Top