473,756 Members | 6,661 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Nullable Types

I've just blogged some stuff on Nullable types in net 2.0.
http://stevenr2.blogspot.com/2006/01...oalescing.html

Question however as to why you can't simply get an implcit conversion from a
..Net value type to to a Sql Server data type rather than having to use null
cheked and DBNull.Value ?

Does anyone have some explanation as to why this is so tricky as to be
included?

http://stevenR2.com
Jan 25 '06 #1
12 3642
of course i meant - as not to be included... :)

"Steven Livingstone" <co*****@NOSPAM .stevenR2.com> wrote in message
news:%2******** *******@tk2msft ngp13.phx.gbl.. .
I've just blogged some stuff on Nullable types in net 2.0.
http://stevenr2.blogspot.com/2006/01...oalescing.html

Question however as to why you can't simply get an implcit conversion from
a .Net value type to to a Sql Server data type rather than having to use
null cheked and DBNull.Value ?

Does anyone have some explanation as to why this is so tricky as to be
included?

http://stevenR2.com

Jan 25 '06 #2
FWIW, there was a discussion a while back where this was considered (at
least a part of VB and you'd have to assume C#).

http://blogs.msdn.com/vbteam/archive...13.aspx#159656

Seems MS considered it. So either i'm missing something and it's already
done, or they chose not to do it.

steven :: http://stevenR2.com
"Steven Livingstone" <co*****@NOSPAM .stevenR2.com> wrote in message
news:%2******** *******@tk2msft ngp13.phx.gbl.. .
I've just blogged some stuff on Nullable types in net 2.0.
http://stevenr2.blogspot.com/2006/01...oalescing.html

Question however as to why you can't simply get an implcit conversion from
a .Net value type to to a Sql Server data type rather than having to use
null cheked and DBNull.Value ?

Does anyone have some explanation as to why this is so tricky as to be
included?

http://stevenR2.com

Jan 25 '06 #3
Steven,

It's not tricky, really. The SQL nullable types were meant as a very
specific solution to a particular domain space. Nullable types are extended
to a much larger space.

Also, I believe that nullable types are meant to be a replacement, not
an interop mechanism.

I don't know that this necessarily justifies it, though.

If anything, I think that the SQL nullables will just go away (meaning,
they will be left in there for backwards compatability), now that a general
solution is in place.

Hope this helps.
"Steven Livingstone" <co*****@NOSPAM .stevenR2.com> wrote in message
news:%2******** *******@tk2msft ngp13.phx.gbl.. .
I've just blogged some stuff on Nullable types in net 2.0.
http://stevenr2.blogspot.com/2006/01...oalescing.html

Question however as to why you can't simply get an implcit conversion from
a .Net value type to to a Sql Server data type rather than having to use
null cheked and DBNull.Value ?

Does anyone have some explanation as to why this is so tricky as to be
included?

http://stevenR2.com

Jan 25 '06 #4
>>Does anyone have some explanation as to why this is so tricky as to be
included?


Will it ever be? In SQL Server NULL is a value type while null in C# is
an uninitialized object. So if I wanted to say this:

xmlDataSet.Tabl es[0].Rows[0]["CurrentSal ary"] = null

Then I am setting that object to null, and not assigning the SQL server
column to the SQL value of NULL. The same goes with passing in a C#
null parameter to a stored proc. C# thinks I am passing in nothing (an
unintialized object) when what I really want to pass in is the SQL
value type of NULL.

_Randal

Jan 25 '06 #5
Well the reason nullable value types were brought out were (in part) because
in C# it isn't always an unitialized *object* you may be passing - i can now
nullify an int? rather than the workarounds of yesteryear. This was created
in part to get round such problems.
But, in setting the value of a stored procedure parameter to NULL (or null
in c#) i think in almost every case you will do a check for null and then
set it to DBNull.Value - if you have exception cases i'd like to hear them.
It just means that i have to check for everything i do.

In your case
xmlDataSet.Tabl es[0].Rows[0]["CurrentSal ary"] = null


this may set the object to null, but if i did (not saying i can, but if)

xmlDataSet.Tabl es[0].Rows[0]["CurrentSal ary"].Value = null

then it would indicate to me that the actual value of the CurrentSalary was
set to null.

In the latter case, it is fairly trivial to then just use DBNull
implicitly - i'd have figured.

But i understand what you are saying and that makes sense also.

steven :: http://stevenR2.com

"Randal" <fr******@swbel l.net> wrote in message
news:11******** **************@ g43g2000cwa.goo glegroups.com.. .
Does anyone have some explanation as to why this is so tricky as to be
included?


Will it ever be? In SQL Server NULL is a value type while null in C# is
an uninitialized object. So if I wanted to say this:

xmlDataSet.Tabl es[0].Rows[0]["CurrentSal ary"] = null

Then I am setting that object to null, and not assigning the SQL server
column to the SQL value of NULL. The same goes with passing in a C#
null parameter to a stored proc. C# thinks I am passing in nothing (an
unintialized object) when what I really want to pass in is the SQL
value type of NULL.

_Randal

Jan 25 '06 #6
Thanks Nicholas - i'd expect them to go too.

I've not read up too much on CLR data types in SQL Server 2005, but it would
have been nice for the C# null to just flow from code to data where maybe a
CLR null was the default NULL in your table columns (even it is were
configurable on install). Hell this would be nice to all the CLR datatypes
that can be supported in sql (int, shorts, strings and so on) - but i guess
going from nvarchar(50) to string requires some fundamenal changes and i'm
just dreaming :) Give me 5 mins and i'll have SQL 2005 being an OO database!

There may well be perfectly good reasons why this is not the case and i'm
never too old to be told why?!

Thanks for the help,
steven :: http://stevenR2.com
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om> wrote in
message news:ut******** ******@TK2MSFTN GP15.phx.gbl...
Steven,

It's not tricky, really. The SQL nullable types were meant as a very
specific solution to a particular domain space. Nullable types are
extended to a much larger space.

Also, I believe that nullable types are meant to be a replacement, not
an interop mechanism.

I don't know that this necessarily justifies it, though.

If anything, I think that the SQL nullables will just go away (meaning,
they will be left in there for backwards compatability), now that a
general solution is in place.

Hope this helps.
"Steven Livingstone" <co*****@NOSPAM .stevenR2.com> wrote in message
news:%2******** *******@tk2msft ngp13.phx.gbl.. .
I've just blogged some stuff on Nullable types in net 2.0.
http://stevenr2.blogspot.com/2006/01...oalescing.html

Question however as to why you can't simply get an implcit conversion
from a .Net value type to to a Sql Server data type rather than having to
use null cheked and DBNull.Value ?

Does anyone have some explanation as to why this is so tricky as to be
included?

http://stevenR2.com


Jan 25 '06 #7
Getting into the guts of it, is it true that nullable types is a struct of an
Int that lives on the stack and has an extra field that says "I'm NULL"?
Anyone have anymore insight on the implementation?

Not sure if this helps but I hear that matching DB types with intrinsic
types is something the C# language team is working on (Whoohoo!) - See below

http://msdn.microsoft.com/msdntv/epi...h/manifest.xml

Sorry I can't offer the correct syntax for you to use in your program. I
would want to do the same thing.
"Randal" wrote:
Does anyone have some explanation as to why this is so tricky as to be
included?


Will it ever be? In SQL Server NULL is a value type while null in C# is
an uninitialized object. So if I wanted to say this:

xmlDataSet.Tabl es[0].Rows[0]["CurrentSal ary"] = null

Then I am setting that object to null, and not assigning the SQL server
column to the SQL value of NULL. The same goes with passing in a C#
null parameter to a stored proc. C# thinks I am passing in nothing (an
unintialized object) when what I really want to pass in is the SQL
value type of NULL.

_Randal

Jan 25 '06 #8
If you ask me, all this null stuff is confusing as heck. I got along just
find with no null value types. First thing Jim Gray does on all his tables
is not allow nulls. Now we also have "IsNull" and "Is Null" to worry about
on CLR UDTs and nullable value types in c#, and Uggg. I would like the old
days (~last year) with value types and ref types and ref types can be null
and get rid of null value types on SQL all together (right, that will
happen).

--
William Stacey [MVP]

"Scottie_do " <Sc*******@disc ussions.microso ft.com> wrote in message
news:29******** *************** ***********@mic rosoft.com...
| Getting into the guts of it, is it true that nullable types is a struct of
an
| Int that lives on the stack and has an extra field that says "I'm NULL"?
| Anyone have anymore insight on the implementation?
|
| Not sure if this helps but I hear that matching DB types with intrinsic
| types is something the C# language team is working on (Whoohoo!) - See
below
|
|
http://msdn.microsoft.com/msdntv/epi...h/manifest.xml
|
| Sorry I can't offer the correct syntax for you to use in your program. I
| would want to do the same thing.
|
|
| "Randal" wrote:
|
| > >>Does anyone have some explanation as to why this is so tricky as to be
| > >>included?
| >
| > Will it ever be? In SQL Server NULL is a value type while null in C# is
| > an uninitialized object. So if I wanted to say this:
| >
| > xmlDataSet.Tabl es[0].Rows[0]["CurrentSal ary"] = null
| >
| > Then I am setting that object to null, and not assigning the SQL server
| > column to the SQL value of NULL. The same goes with passing in a C#
| > null parameter to a stored proc. C# thinks I am passing in nothing (an
| > unintialized object) when what I really want to pass in is the SQL
| > value type of NULL.
| >
| > _Randal
| >
| >
Jan 26 '06 #9
William Stacey [MVP] <wi************ @gmail.com> wrote:
If you ask me, all this null stuff is confusing as heck. I got along just
find with no null value types. First thing Jim Gray does on all his tables
is not allow nulls. Now we also have "IsNull" and "Is Null" to worry about
on CLR UDTs and nullable value types in c#, and Uggg. I would like the old
days (~last year) with value types and ref types and ref types can be null
and get rid of null value types on SQL all together (right, that will
happen).


I'm perfectly happy with nullable types - but we're missing one part of
the quadrant, which is to be able to declare reference type variables
which can *never* be null:

string! x = "hello"; // Fine
string! x = null; // Compiler complains

(Just as an example.)

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 26 '06 #10

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

Similar topics

10
1832
by: John Wood | last post by:
I was just looking at an article about using nullable value types. (value types that can effectively have no value and not be set). The syntax is to append a question-mark to the value type in the declaration, eg: int? age; I don't like that much, I think it would be much more consistent to use a new keyword, such as "nullable". But anyways...
6
3843
by: Steven Livingstone | last post by:
Bit of advice here folks. I am creating a default constructor that just initializes a new instance of my object and a secon constructor that takes an ID and loads an object with data from the database for the given ID. I could just leave two separate constructors, or i could just have the default one call the second with a "-1" as the ID. This means that the initialization of certain fields can be based on whether my ID is -1 (for lazy...
8
4856
by: shawnk | last post by:
Given several nullable boolean flags; bool? l_flg_01 = true; bool? l_flg_02 = false; bool? l_flg_03 = true; bool? l_result_flg = null; I would have liked one of these syntax formats to work; // if ( l_flg_01 && l_flg_02 && l_flg_03 ) // Line A
0
1403
by: Larry Lard | last post by:
There seems to be something a bit lacking in the way the dataset designer thing deals (or rather doesn't) with nullable fields in VS2005. Maybe it's cos I'm using VB2005 Express (which is variously crippled), I don't know. But it seems to me obvious that there should be some way to specify that DB fields which can be null should be mapped to appropriate Nullable(Of T) generic types. For fields there's a dropdown in the dataset designer...
1
5678
by: Joe Bloggs | last post by:
Hi, Can someone please kindly show me how to determine if a type (read value type) is Nullable. MSDN has this KB: How to: Identify a Nullable Type (C# Programming Guide) http://msdn2.microsoft.com/en-us/library/ms366789.aspx however, using their code snippet, I couldn't get it to work:
8
10702
by: Sam Kong | last post by:
Hello, I want to define a generic class which should accept only nullable types or reference types. What's the best way to costrain it? --------- class MyClass<T>{ ...
3
2591
by: Mike P | last post by:
What are nullable types used for? The only use I can think of is where I have a method like this where some values being passed to it may be null : public int AddUser(int? UserRegion, string UserName etc) You might have an Add User page where some of the fields do not have a value entered by the user, so you can account for this by making these types nullable in the method above. Is this the right usage of nullable types? And what...
3
2269
by: MobileBoy36 | last post by:
Hi all, Nullable types were announced as new handy stuff in .NET 2.0 But it seems like the datareader doesn't support nullable types. You have still to check for "IsDbNull". So, are Nullable Types useful at this moment? Do you get any benefit using them (at this moment)? Is Microsoft going to change this? Best regards,
6
1471
by: Tony Johansson | last post by:
Hello! I'm reading in a book called Visual C# 2005. In the chapter about Generics there ia a section about Nullable types. Here is the text that isn't complete true I think. It says: "You can also look at the value of a reference type using the Value property. If HasValue is true, then you are guarantee a non-null value for Value, but if HasValue is false, that is, null has been assigned to the variable, then accessing Value will
0
9275
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9873
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9713
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8713
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7248
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6534
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5304
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3359
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2666
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.