Nullable Types are NOT Integrated in .NET v2.0

Nullable value types have been added to .NET v2.0, but they have NOT really been integrated.  The C# team has done a good job incorporating support for nullable types into the syntax, but the VB team has done nothing, which once again leaves VB behind the curve in features.  But that is not what this post is about -- this post is about the fact that nullable types have NOT been integrated into the rest of the .NET framework -- and obvious places at that.

(1) Reading Data: Given a data reader, you can't just assign a value to a nullable type!  This one just seems incomprehensible -- nullables obviously already exist in the database, and the .NET SqlTypes support them already, so this seems to be a key integration point.  But alas, you simply can NOT assign a value from your data reader to a nullable type.  Instead, you still have to explicitly test for DBNull, and handle that case separately:
 
    int? nullInt;
    if (dataReader.IsDBNull(index)) { nullInt = null; }
    else { nullInt = dataReader.GetInt32(index); }

Note that Julia Lerman observed this before -- see her comments for more viewpoints.

(2) ConvertType: C# does at least support casts to/from regular types and nullable types, and it even gives you a handy way to convert nulls to a default by using the ?? operator, but just try to do any generic type conversions with Convert.ChangeType -- not supported!  Again this seems like an obvious integration point, if nullable types are to be "builtin", but once again the new nullable types just feel like an afterthought -- and NOT integrated.

(3) WinForm Controls: The official story is that the data grids support nullable types, but what happens when you try to use a NumericUpDown or a DateTimePicker -- not supported!  That's right, once again you'll find that you still have no real integration for nulls, and you'll have to continue resorting to magic values or checkboxes and manual conversions.  These controls seem like another obvious place where there should be support for nullables.

All of this lack of integration makes me wonder what the point of nullable types really is?  We could always create our own structs that supported the concept of null very similarly, but they lacked the builtin syntax support that C# is including, not to mention they failed to work with data-binding in grids, but the new nullable types simply aren't much better.  The one thing they have going for them is that they will at least be a "standard" nullable.

Note that none of these issues prevented me from adding support for nullable types to my O/R Mapper, but I just thought I'd pass along what I observed since I do think the support I discovered was very disappointing.
Published Tuesday, May 03, 2005 4:10 PM by PaulWilson

Comments

# re: Nullable Types are NOT Integrated in .NET v2.0

Well, since the idea of nullable value types was just plain left out of a modern platform (.NET) in the beginning was incredibly bizarre to me, especially in this age of database interactivity.

Getting nullables to be more integrated is a definitely great start to get the platform (.NET) into CURRENT standards, instead of OO-based C++ standards of 10 years ago (where Objects would serialize/deserialize locally...)

Agreed these "irritations" with the DataReaders is annoying, and IMO the IDataReader interfaces should obviously be extended (via IDataReader2 or IDataReaderEx or something) to provide nullable types through the interface, and hence "encourages" implementing it all the way through to the SqlDataReader's implementation of
"int? IDataReader2.GetInt32(int ordinal)"

Tuesday, May 03, 2005 4:23 PM by Eric Newton

# re: Nullable Types are NOT Integrated in .NET v2.0

Just say no to NULLS!

Join PAN. Programmers Against Nulls!

Tuesday, May 03, 2005 6:09 PM by NotNull

# re: Nullable Types are NOT Integrated in .NET v2.0

It's not just IDataReader that's not supported. NONE of the System.Data classes support nullable types. Just try to use something like

command.Parameters.AddWithValue("@SomeParam", (int?)null)

This will throw an exception once you try to execute the command. Yet the following works:

command.Parameters.AddWithValue("@SomeParam", SqlInt32.Null)

There aren't even any implicit/explicit conversions between SqlTypes and Nullable types. I was baffled when I noticed that.

It's things like this that make nullable types *really* annoying to work with when you're doing database work.

Erm, refresh my memory, why were they added again?

Tuesday, May 03, 2005 6:21 PM by Ruben

# re: Nullable Types are NOT Integrated in .NET v2.0

Checkbox feature. Let's hope something better shows up in before final release.

Tuesday, May 03, 2005 7:26 PM by foobar

# re: Nullable Types are NOT Integrated in .NET v2.0

Well, one of the major problems is the idea of NULL in the database in the first place.

One of my major complaints about the SQL Server 2000 table design tools is that columns allow NULL by default. This is a pretty significant design decision, and I believe that NULLs should be allowed in the database only in extra-ordinary circumstances.

However, many 'DBAs' allow null by default, and it really creates havoc.

NULLable types are a hack to make programming against screwy database designs easier for client-side programmers.

Tuesday, May 03, 2005 11:02 PM by Jerry Dennany

# re: Nullable Types are NOT Integrated in .NET v2.0

You know, while you ARE right....

...personally I do not care about the database integration at all. I use an O/R mapper fo rall my database work, including parameters, and I thus can add all the integration and conversion code within MY O/R mapper - so parameters etc. will work.

What really makes me upset is the other part. our points (2) and (3) are in levels that the USER of the O/R mapper has to work with. And there it will come back and hit them. There is no way I can hide (yet again) databinding efficiencies. There is no way I can handle the convert issue.

And this really shows how much an afterthought this is. MS drops the ball again (on databinding). Are they the only company which will take a dozen or so attempts o get this right?

Tuesday, May 03, 2005 11:43 PM by Thomas Tomiczek

# re: Nullable Types are NOT Integrated in .NET v2.0

Last year at Teched Europe, someone asked Dan Fernandez about nullable types and datareader... they (he and another MS guy) got that look on their face: "oh erm... whoa, we didn't think of that! we'll pass it on!". Nothing happened apparently.

Oh, btw, DON'T use datareader.IsDBNull(..). It's very slow. Test the value for System.DBNull.Value, much faster. (magnitudes faster)

I also find the lack of integration of nullable types a severe stupidity. I mean, int's and all ARE already structs. All it takes is another field. ONE FIELD.

ok, you can use Nullable<T> but that's almost 50% slower. Why, I don't know, as it's just 1 flag, 1 BIT.

Wednesday, May 04, 2005 3:03 AM by Frans Bouma

# NotNull

Wednesday, May 04, 2005 5:05 AM by TrackBack

# re: Nullable Types are NOT Integrated in .NET v2.0

Check out NullableTypes on SourceForge. Works with .NET 1.1 and VB providing useful conversion functions etc.

http://nullabletypes.sourceforge.net

Wednesday, May 04, 2005 6:10 AM by Damien Guard

# re: Nullable Types are NOT Integrated in .NET v2.0

Paul,

Wow, thanks for the headsup.

Wally

Wednesday, May 04, 2005 6:46 AM by Wallym

# re: Nullable Types are NOT Integrated in .NET v2.0

WTF?
I thought that interaction with nullable fields in the DB (such as situation #1) was the main line for nullable types in the framework. Mostly to eliminate all kinds of null checking.

If they don't do that then why even spend the time to implement it?


?UsefullType

Wednesday, May 04, 2005 9:17 AM by AndrewSeven

# re: Nullable Types are NOT Integrated in .NET v2.0

I think the issue at the core here is that nullable types are never actually nulls.

int? i = null;

Assert(i == null);
Assert((object)i == null); // WILL FAIL

nullable types are simple objects with a flag. I think because all those methods take and return base Object type, when type casted to a nullable type with a "null" value, you wouldn't get a null in result.

I think the problem is not integration, but how the nullable types are implemented in the first place.

That's at least how I understand it.
http://blog.dreamprojections.com/archive/2005/04/10/787.aspx

Wednesday, May 04, 2005 12:25 PM by Alex

# re: Nullable Types are NOT Integrated in .NET v2.0

I don't think anyone expects them to be null -- just to be integrated with their expected uses.

Wednesday, May 04, 2005 12:45 PM by Paul Wilson

# re: Nullable Types are NOT Integrated in .NET v2.0

Wednesday, May 04, 2005 4:24 PM by Wilco Bauwer

# Nullable Types are NOT Integrated into .Net V2.0 (not Beta 2 atleast)

Wednesday, May 04, 2005 7:39 PM by TrackBack

# re: Nullable Types are NOT Integrated in .NET v2.0

I've been reading up on the subject...........And the impedance mismatch continues.

Friday, May 06, 2005 8:28 AM by Wallym

# Convert.ChangeType doesn't handle nullables

I had an assembly, originally written against .NET 1.1 and now running
on .NET 2.0, that tried to convert...

Wednesday, May 31, 2006 11:41 AM by Peter Johnson's Blog

# re: Nullable Types are NOT Integrated in .NET v2.0

I use a simple converter for O/R mapping: public static class SqlTypeConverter { public static int? ToNulllableInt32(object o) { SqlInt32 i = (SqlInt32)o; return i.IsNull ? null : (int?)i; } ...or public static int? ToNullable(SqlInt32 i) { return i.IsNull ? null : (int?)i; } ... and so on ... } But, of course, this still doesn't help with the ignorant controls!

Sunday, September 03, 2006 9:29 PM by bstabile

# re: Nullable Types are NOT Integrated in .NET v2.0

Here's how I get around the <null> value in a datareader:

if (myDataReader.GetValue(5).ToString().Length > 0)

{

do stuff

}

ive only used to to test the selected value of a dropdown list when I was populating it from the database and getting the value that had been selected by the user a previous time.

worked like a champ.

the reason I used that if statement was because when I tried to write that value to the page, nothing happened.  So, since testing != null or != "" didnt work, I went with length

Friday, December 01, 2006 3:54 PM by Mortikhi

# re: Nullable Types are NOT Integrated in .NET v2.0

Check against the DBNull object, not null.

Sunday, December 03, 2006 5:21 PM by PaulWilson

# re: Nullable Types are NOT Integrated in .NET v2.0

Although it's true that the lack of integration is annoying, the fact that I CAN now create a null-value base type is a tremendous relief.  I don't mind using obscure conversion syntax at the border of the data-retrieval-layer if I don't have to pollute the rest of my code with all those ugly SqlTypes.

Le roi est nul!  Vive le roi!

Monday, December 18, 2006 1:19 PM by Mike Bridge

# re: Nullable Types are NOT Integrated in .NET v2.0

Work-around for NumericUpDown and databinding to dbnull.

Here is the crappy workaround I came up with to handle the fact that Nup controls silently fail on databinding to DBNull.

My scenario is that I have a DataGridView and when the user moves from row to row I display some other information and some of that information is displayed in NumericUpDown controls.

On the WinForm I add a hidden textbox control right under the Nup control and set its DataBindings to be the same as those of the Nup control.

In my SelectionChanged event handler for the dgv, I have check the textbox to see if its nullorEmpty and if so then call the Nup's ResetText and set its Value to be zero, else set its value to be the value in the textbox.

This seems to work.

Sample of my code:

if (string.IsNullOrEmpty(this.TxtLIWage.Text))

{

   this.NupLIWage.Value = 0m;

   this.NupLIWage.ResetText();

}

else

{

   this.NupLIWage.Value = StringToNumber.ConvertToDecimal(this.TxtLIWage.Text);

}

The StringToNumber is my own class that handles converting text to decimal and takes care to culture (Canadian English/French).

This crappy workaround might also come in handy for dealing the DateTimePicker controls too.

Hope this is helpful to others.

Sheir

Tuesday, October 28, 2008 5:45 PM by sheir

# abusing.me &raquo; Blog Archive &raquo; Framework UI - DataBindings und Nullable&lt;T&gt;

Pingback from  abusing.me  &raquo; Blog Archive   &raquo; Framework UI - DataBindings und Nullable&lt;T&gt;

# Nullable Types in .Net 2.0 &laquo; CollectedDotNet

Pingback from  Nullable Types in .Net 2.0 &laquo; CollectedDotNet

Tuesday, April 28, 2009 10:25 PM by Nullable Types in .Net 2.0 « CollectedDotNet

Leave a Comment

(required) 
(required) 
(optional)
(required)