Tim Laughlin's Everything VB.NET Blog


Nullable Generic Structure

I discovered a small gem within the .net 2.0 framework, system.nullable.  This structure takes a parameter of the underlying data type you want to use.  So for example you can now set a integer to nothing rather then setting it some value and trying to remember through out your code that the value should be treated as non existent or null.

This especially useful in database applications where a column within a table accepts nulls.  If wanted to set that value back to null you can now so easily.

For example

Declare a nullable Integer
Dim IntMyVar As Nullable(Of Integer)

See if it has a value

if  IntMyVar.HasValue then
' We  now know if we  have an integer
else
' We now know we have a null or nothing
end if

' set a value
IntMyVar = 10

'Set back to null
IntMyVar = Nothing

'Use with default, if nothing.

debug.writeline(IntMyVar.GetValueOrDefault(3))

Hope that you find this bit of info helpful.