|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Why have a DBNull?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 "Bob" <no***@nowhere.com> schrieb: Can you show a sample where 'Nothing' is treated as 'DBNull'?> 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)? -- M S Herfried K. Wagner M V P <URL:http://dotnet.mvps.org/> V B <URL:http://classicvb.org/petition/> 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/default.asp?url=/library/en-us/cpref/html/frlrfsystemdatadatarowcollectionclassaddtopic2.asp Hope this helps Jay Show quoteHide quote "Bob" <no***@nowhere.com> wrote in message news:OxgtOqKNFHA.3000@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 > > 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. Show quoteHide quote "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/default.asp?url=/library/en-us/cpref/html/frlrfsystemdatadatarowcollectionclassaddtopic2.asp > > Hope this helps > Jay > > "Bob" <no***@nowhere.com> wrote in message > news:OxgtOqKNFHA.3000@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 > > > > > > > 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 Unlike C++, VB.NET & C# requires an object be present when you call an > doesn't a string variable used like b.Trim return nothing when b is > nothing > instead of an exception? 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 I rarely check a variable for being nothing before I call instance methods & > check it for being nothing unless 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 Show quoteHide quote "Dennis" <Den***@discussions.microsoft.com> wrote in message news:46DDBD73-3F3B-4D4C-8C46-5E98904C8E2A@microsoft.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/default.asp?url=/library/en-us/cpref/html/frlrfsystemdatadatarowcollectionclassaddtopic2.asp >> >> Hope this helps >> Jay >> >> "Bob" <no***@nowhere.com> wrote in message >> news:OxgtOqKNFHA.3000@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 >> > >> > >> >> >> 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. :-))) CorSo that the woman can tell the man what to do :)
Show quoteHide quote "Cor Ligthert" <notmyfirstn***@planet.nl> wrote in message news:ubCNdwPNFHA.4052@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 > On 2005-03-30, Dennis <Den***@discussions.microsoft.com> wrote:
> To go farther, why That's a good question, actually, and it goes deep into different> 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. 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.
Show quote
Hide quote
"David" <dfos***@woofix.local.dom> wrote in message I personally have never come across a case when it was useful for me to havenews:slrnd4k6v2.9d7.dfoster@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. <...> 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 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]" <Jay_Harlow_***@msn.com> wrote in message http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdatadatarowcollectionclassaddtopic2.aspnews:eablBHLNFHA.3192@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. > > Show quoteHide quote > > Hope this helps > Jay 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 I agree, the only thing that Nothing saves here is defining a second Special | case is also inconsistent and confusing. 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 Show quoteHide quote "Bob" <no***@nowhere.com> wrote in message http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdatadatarowcollectionclassaddtopic2.aspnews:eR0J3aTNFHA.4052@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]" <Jay_Harlow_***@msn.com> wrote in message | news:eablBHLNFHA.3192@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. | > | > | Show quoteHide quote | > | > Hope this helps | > Jay | |
vb.net windows deployment
Datagrid cell change event? Where to find event. Inheritance Question in VB.NET Please: Sign the Petition in order to Save VB6 code... CSV Separator Assign deployment version number to a variable RowFilter and DataColumn Named as Number Databindings Question Problems with the Masked Edit Control customised datagrid |
|||||||||||||||||||||||