vrijdag, december 16, 2005

 

to null or not to null, c# 2.0 makes it possible

recently I had a discussion of how to handle null variables/datafields. Personally I always tried to avoid these cases but now C# 2.0 has a new feature, called nullable type. Look at the next sample:

int? myInt;

if(dbReader.IsDBNull(dbReader.getOrdinal("whatever")))
myInt = null;
else
myInt = System.Conver.ToInt32(dbReader["whatever"]);

Bassically myInt is a simple struct with a bit flag to check 4 null...

nice clean solution i guess..

!! note the question mark !! > int?
note: try to write more articles in english, for a bigger audiance :-)

- edit -

vb variant:
dim myInt as Nullable(Of Integer)

myInt = nothing

if myInt.equals(nothing) then
'null
else
'data
end if

not so smooth as the C# flavour, more homegrown style :-(