Nullable types - useful for databases?

by Dominic Zukiewicz 21. August 2007 11:34

Overview

Before .NET 2.0, if you wanted to choose a value that represented null, you had to fall back on the xxx.MinValue or xxx.MaxValue and then convert this value to NULL once the information was hitting the database.

In order to make it easier, Microsoft thought that it was helpful to allow value types to be assigned null and therefore just plonk the value straight into database - correct!? Well ..... sort of.

Conversion

I've found nullable types aren't all "that". Lets take a quick example to see how they can be a handful to use:

int? input = null;

string strInput = Console.ReadLine();

//Won't work - can't convert from "out int" to "out int?"
if( ! int.TryParse(strInput, out input) )
{
     .....
}

So we can't cast to them directly, bit of a pain, so we'll just have to add another variable in.

int tempInput = 0;
int? input = null;

string strInput = Console.ReadLine();

if(int.TryParse(strInput, out tempInput))
{
    input = tempInput;
}

Bit of a pain, but I'll live.

DataTables - assigning null values

What about DataTables, does that make it easier to assign values? Well, lets take a look at that too, which was something I was only too pleased about finding out.

One of the most obvious advantages is that the columns are allowed NULL values, this would be a perfect example of where to use it.

int tempInput = 0;
int? input = null;

string strInput = Console.ReadLine();

if(int.TryParse(strInput, out tempInput))
{
    input = tempInput;
}

DataTable dt = new DataTable();
dt.Columns.Add("Input", typeof( int ));
dt.Columns["Input"].AllowDBNull = true;

dt["Input"] = input;   //Runtime error!!!

Runtime error!? Well, you can convert an int? to an int. Even though the nullable type may have a null value, the DataColumn will only allow the DBNull.Value to be assigned.

"How do we get around this? Aha - I'll use the shortcut if statement - you can't fool me!"

int tempInput = 0;
int? input = null;

string strInput = Console.ReadLine();

if(int.TryParse(strInput, out tempInput))
{
    input = tempInput;
}

DataTable dt = new DataTable();
dt.Columns.Add("Input", typeof( int ));
dt.Columns["Input"].AllowDBNull = true;

DataRow dr = dt.NewRow();
dr["Input"] = (input == null? DBNull.Value : input) ;  //Compile error!

I applaud your bravery! Its what I did too. But the problem is that the shortcut if statement must return the same Type, regardless of which IF is executed. Even if you were assigning to an Object object, it still wouldn't work.

object outcome = ( 1==1 ? 1m : 1f); //Compiler error!

"So, we can't assign null directly. We can't use the shortcut if decision. What else can we do?"

The only other option is to write out the logic, in full, in an if statement.

if( input.HasValue )
     dr["Input"] = input;
else
     dr["Input"] = DBNull.Value;

Therefore, we are back to square one, with regards to the logic, as we are still having to convert it to DBNull.Value eventually - what a nightmare! So the question is - did nullable types pose any benefit?? Thoughts please!

Tags:

Framework

Powered by BlogEngine.NET 1.5.0.7
Theme by Interakting

Interakting

A full service digital agency offering online strategy, design and usability, systems integration and online marketing services that deliver real business benefits and ensure your online objectives are met.

Calendar

<<  February 2012  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
2728291234
567891011

View posts in large calendar