473,239 Members | 1,491 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,239 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 2390
"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: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.