473,320 Members | 1,854 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Why have a DBNull?

Bob
I'm sure there's a good reason, I just haven't been able to think of it -
why didn't MS allow "DBNull" values to simply be a null (Nothing)?

Bob
Nov 21 '05 #1
10 2393
"Bob" <no***@nowhere.com> schrieb:
I'm sure there's a good reason, I just haven't been able to think of it -
why didn't MS allow "DBNull" values to simply be a null (Nothing)?


Can you show a sample where 'Nothing' is treated as 'DBNull'?

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Nov 21 '05 #2
Bob,
It allows DBNull to be treated differently then Nothing.

For example DataRowCollection.Add uses "Nothing" to mean set the column to
its Default value, while it uses DBNull to mean set it to a Database Null.

http://msdn.microsoft.com/library/de...saddtopic2.asp

Hope this helps
Jay

"Bob" <no***@nowhere.com> wrote in message
news:Ox**************@TK2MSFTNGP10.phx.gbl...
I'm sure there's a good reason, I just haven't been able to think of it -
why didn't MS allow "DBNull" values to simply be a null (Nothing)?

Bob

Nov 21 '05 #3
I think the question was why do we have to contend with DBNull and
Nothing..why can't they be treated the same in code so you don't have to
check for something being a dbnull as well as nothing. To go farther, why
doesn't a string variable used like b.Trim return nothing when b is nothing
instead of an exception? Everytime you want to trim a string, you have to
check it for being nothing unless you use the Trim function from Microsoft
Visual Basic name space.

"Jay B. Harlow [MVP - Outlook]" wrote:
Bob,
It allows DBNull to be treated differently then Nothing.

For example DataRowCollection.Add uses "Nothing" to mean set the column to
its Default value, while it uses DBNull to mean set it to a Database Null.

http://msdn.microsoft.com/library/de...saddtopic2.asp

Hope this helps
Jay

"Bob" <no***@nowhere.com> wrote in message
news:Ox**************@TK2MSFTNGP10.phx.gbl...
I'm sure there's a good reason, I just haven't been able to think of it -
why didn't MS allow "DBNull" values to simply be a null (Nothing)?

Bob


Nov 21 '05 #4
Dennis,
As I stated it allows DBNull to be treated differently then Nothing. A
second reason is to allow your code to avoid Nothing itself.

Remember DBNull is simply an implementation of the Special Case pattern.

http://martinfowler.com/eaaCatalog/specialCase.html

It allows you to write code that you know will always have a value, so you
are able to call any of its base methods (in this case Object).

For example I know I can call ToString on each value returned from
DataRow.Item() as those items will never be Nothing. If one of the values
could be Nothing I would need special case code to call Object.ToString...
Yes DBNull may require special case codes in spots, I'm not disputing
that...

To go farther, why
doesn't a string variable used like b.Trim return nothing when b is
nothing
instead of an exception? Unlike C++, VB.NET & C# requires an object be present when you call an
instance method on that object. IMHO at a certain "OO" level it doesn't
really make sense to ask an object to perform some behavior when there is no
object there to ask do to anything...

Unmanaged C++ (I'm not sure about managed C++) does allow calling instance
methods on null (Nothing) pointers causing this (Me) to be null (Nothing).
However it normally required a lot of "AssertValid" macros in your code to
ensure that you actually have an object that you are calling a method on. In
other words 99% or better of code requires a valid object, and one or two
methods could benefit from being Nothing...
Everytime you want to trim a string, you have to
check it for being nothing unless I rarely check a variable for being nothing before I call instance methods &
I rarely receive NullReferenceExceptions, as I make sure variables are
initialized, I normally have strategic Guard conditions or use the Null
Object Pattern to prevent variables from being Nothing. This is not to say I
don't receive NullReferenceExceptions during development, its just that they
are "limited"...

Current versions of C# & VB.NET 2005 will issue compile warnings if you try
to call an instance method on an un-initialized variable (the most common
reason for NullReferenceExceptions).

Hope this helps
Jay
"Dennis" <De****@discussions.microsoft.com> wrote in message
news:46**********************************@microsof t.com...I think the question was why do we have to contend with DBNull and
Nothing..why can't they be treated the same in code so you don't have to
check for something being a dbnull as well as nothing. To go farther, why
doesn't a string variable used like b.Trim return nothing when b is
nothing
instead of an exception? Everytime you want to trim a string, you have to
check it for being nothing unless you use the Trim function from Microsoft
Visual Basic name space.

"Jay B. Harlow [MVP - Outlook]" wrote:
Bob,
It allows DBNull to be treated differently then Nothing.

For example DataRowCollection.Add uses "Nothing" to mean set the column
to
its Default value, while it uses DBNull to mean set it to a Database
Null.

http://msdn.microsoft.com/library/de...saddtopic2.asp

Hope this helps
Jay

"Bob" <no***@nowhere.com> wrote in message
news:Ox**************@TK2MSFTNGP10.phx.gbl...
> I'm sure there's a good reason, I just haven't been able to think of
> it -
> why didn't MS allow "DBNull" values to simply be a null (Nothing)?
>
> Bob
>
>


Nov 21 '05 #5
Dennis,

Why are there from almost all living creatures on earth a man and a woman.

My expirience is that it is very difficult to change behaviours back in time
and often there is a good reason to let it as it is.

:-)))

Cor
Nov 21 '05 #6
So that the woman can tell the man what to do :)
"Cor Ligthert" <no************@planet.nl> wrote in message
news:ub**************@TK2MSFTNGP12.phx.gbl...
Dennis,

Why are there from almost all living creatures on earth a man and a woman.

My expirience is that it is very difficult to change behaviours back in
time and often there is a good reason to let it as it is.

:-)))

Cor

Nov 21 '05 #7
On 2005-03-30, Dennis <De****@discussions.microsoft.com> wrote:
To go farther, why
doesn't a string variable used like b.Trim return nothing when b is nothing
instead of an exception? Everytime you want to trim a string, you have to
check it for being nothing unless you use the Trim function from Microsoft
Visual Basic name space.


That's a good question, actually, and it goes deep into different
approaches to programming, and why some people find much of the VB
namespace along with VB's special-casing of string operators dangerous
while others find it indispensable.

On the one side is the ease-of-use crowd, asking questions like the one
above. If a string is Nothing, there's no reason that string functions
can't return something reasonable. And generally they're absolutely
right, since there's almost always a reasonable semantic equivalence
between Nothing and String.Empty.

On the flipside of the question is a completely different way of looking
at the problem. The question here is why am I dealing with an
uninitialized string, and why is the runtime hiding that from me? If a
string is Nothing when it shouldn't be, that's usually the result of
deeper design issues, and I want to know about it as early in
development as possible. The fact that Trim() hides this fact from me
is an error in design, not a feature.

And in a well-designed program, if a value should never be Nothing, a
check for that is limited to IO classes, not spread throughout the
program.

VB.Classic tended to adhere to the former point of view, C++/Java to the
latter point of view. Since .Net was geared more towards the C++/Java
crowd, the core framework throws on Nothing, while the VB namespace and
VB.Net itself often treats empty strings and null strings equivalently.

Nov 21 '05 #8
Bob
That would seem to be to be poor design. It would have been better to have
to use something like "DBDefault.Value" to make it clear what will happen.
Nothing can never be found in actual table data, so allowing Nothing is this
case is also inconsistent and confusing.

Bob

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:ea**************@TK2MSFTNGP10.phx.gbl...
Bob,
It allows DBNull to be treated differently then Nothing.

For example DataRowCollection.Add uses "Nothing" to mean set the column to
its Default value, while it uses DBNull to mean set it to a Database Null.

http://msdn.microsoft.com/library/de...saddtopic2.asp
Hope this helps
Jay

Nov 21 '05 #9
Bob
"David" <df*****@woofix.local.dom> wrote in message
news:slrnd4k6v2.9d7.df*****@woofix.local.dom...
<...>
On the flipside of the question is a completely different way of looking
at the problem. The question here is why am I dealing with an
uninitialized string, and why is the runtime hiding that from me? If a
string is Nothing when it shouldn't be, that's usually the result of
deeper design issues, and I want to know about it as early in
development as possible. The fact that Trim() hides this fact from me
is an error in design, not a feature.

And in a well-designed program, if a value should never be Nothing, a
check for that is limited to IO classes, not spread throughout the
program.

<...>

I personally have never come across a case when it was useful for me to have
a String as Nothing instead of "". I wish they had made String a structure,
although I'm sure there are some fundamental reasons why it isn't.

Bob
Nov 21 '05 #10
Bob
| Nothing can never be found in actual table data
That's the entire point being have DBNull! Nothing itself is not allowed in
the table data itself!

| , so allowing Nothing is this
| case is also inconsistent and confusing.
I agree, the only thing that Nothing saves here is defining a second Special
Case pattern object (DBDefault). I too wonder if its worth saving defining
DBDefault?

However! Rather then define both DBNull & DBDefault types, I would consider
defining a single DBValue type & have 2 fields on it DBValue.Null &
DBValue.Default.

Hope this helps
Jay

"Bob" <no***@nowhere.com> wrote in message
news:eR**************@TK2MSFTNGP12.phx.gbl...
| That would seem to be to be poor design. It would have been better to have
| to use something like "DBDefault.Value" to make it clear what will happen.
| Nothing can never be found in actual table data, so allowing Nothing is
this
| case is also inconsistent and confusing.
|
| Bob
|
| "Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
| news:ea**************@TK2MSFTNGP10.phx.gbl...
| > Bob,
| > It allows DBNull to be treated differently then Nothing.
| >
| > For example DataRowCollection.Add uses "Nothing" to mean set the column
to
| > its Default value, while it uses DBNull to mean set it to a Database
Null.
| >
| >
|
http://msdn.microsoft.com/library/de...saddtopic2.asp
| >
| > Hope this helps
| > Jay
|
|
Nov 21 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: DraguVaso | last post by:
Hi, Something I don't understand about a Typed DataSet: When a value in the DataSet is DBNull, it throws this error: "Cannot get value because it is DBNull". But aren't Typed DataSets...
4
by: Michael Culley | last post by:
Which is better: SomeValue is DBNull or SomeValue == DBNull.Value Thanks, Michael Culley
11
by: Patrick.O.Ige | last post by:
When i try and use this (Where Unit is a column in my Table):- If Unit Is DBNull.Value Then Return "1" Else Return "2" End If I always have 2 returned! Even when Unit is NULL! I want a...
4
by: Tina | last post by:
I have instantiated an insertRow for a dataset. I now want to make the GLCode DBNull. I have tried: insertRow.GLCode = Convert.dbnull insertRow.GLCode = Convert.dbnull(insertRow.GLCode) and...
8
by: fredda054 | last post by:
Hi everybody ! I have a little repeater/hyperlink issue I'm not sure how to solve. I use a repeater to list subjects available at a specific school. The datasource of this repeater is a...
8
by: MattB | last post by:
Hello. I have a vb.net (asp.net) application that uses ado.net datasets. At one point, I need to check a text field in a DataTable to see if there's any text in it before performing text operations...
6
by: tshad | last post by:
I have a value coming from my Object (or it could also be from a SqlDbReader) where I need to test for DBNull and 0. I tried to do it in one call: if (not (newPosition.ReportsTo is...
6
by: cj | last post by:
in the command window I get: ? result {System.DBNull} : {System.DBNull} I need to check for this in code. I then tested in the command window if result = system.DBNull 'DBNull' is a type...
19
by: Dave | last post by:
If Iwant to check if dataset1.SelectQuery1.column1 == System.DBNull.Value. How do I do this? What I wrote above will give an error. -- L. A. Jones
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.