.NET Forum / Windows Forms / WinForm General / December 2004
What is Nothing?
|
|
Thread rating:  |
Fred Herring - 20 Dec 2004 16:15 GMT 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
John Saunders - 20 Dec 2004 16:29 GMT >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
Fred Herring - 20 Dec 2004 17:07 GMT 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
> >I need to understand some basic definitions for things like null, dbnull, > >and [quoted text clipped - 18 lines] > I hope that helps. > John Saunders John Saunders - 20 Dec 2004 17:55 GMT > Thanks for the quick reply to my nothing question. I would like to follow > up [quoted text clipped - 6 lines] > 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
Bruce Wood - 20 Dec 2004 20:02 GMT 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."
John Saunders - 20 Dec 2004 21:56 GMT >I think that John missed one important point. > [quoted text clipped - 18 lines] > 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.
Jon Skeet [C# MVP] - 20 Dec 2004 23:37 GMT > 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...
 Signature Jon Skeet - <skeet@pobox.com> http://www.pobox.com/~skeet If replying to the group, please do not mail me too
John Saunders - 21 Dec 2004 17:01 GMT >> First, null (or Nothing) in .NET is what a reference type contains when >> it [quoted text clipped - 14 lines] > 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
Jon Skeet [C# MVP] - 21 Dec 2004 17:54 GMT > > 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 [quoted text clipped - 3 lines] > > 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
 Signature Jon Skeet - <skeet@pobox.com> http://www.pobox.com/~skeet If replying to the group, please do not mail me too
Ravichandran J.V. - 22 Dec 2004 08:35 GMT Nothing readies an object for collection.
with regards,
J.V.Ravichandran
 Signature - http://www.geocities.com/ jvravichandran - http://www.411asp.net/func/search? qry=Ravichandran+J.V.&cob=aspnetpro - http://www.southasianoutlook.com - http://www.MSDNAA.Net - http://www.csharphelp.com - http://www.poetry.com/Publications/ display.asp?ID=P3966388&BN=999&PN=2 - Or, just search on "J.V.Ravichandran" at http://www.Google.com
John Saunders - 22 Dec 2004 13:21 GMT > 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
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|