Cool VB.NET Features – Nullable Types

All of the built-in types are nullable–not just String!

Good programmers know that an empty string is not the same as an unknown string and zero is not the same as an undefined value.  This distinction is what makes nullable types so useful.

 

Strings are Nullable

I unwittingly used one nullable type from the beginning because the String type is a nullable type.  For example, the code below will print out “tmpString1 is set to Nothing”.

Dim tmpString1 As String

If tmpString1 Is Nothing Then
    Console.WriteLine("tmpString1 is set to Nothing")
ElseIf tmpString1 = "" Then
    Console.WriteLine("tmpString1 is the empty string")
Else
    Console.WriteLine("tmpString1 is neither the the empty string nor is it Nothing")
End If

And the code below will print out “tmpString2 is the empty string”:

Dim tmpString2 As String = ""

If tmpString2 Is Nothing Then
    Console.WriteLine("tmpString2 is set to Nothing")
ElseIf tmpString2 = "" Then
    Console.WriteLine("tmpString2 is the empty string")
Else
    Console.WriteLine("tmpString2 is neither the the empty string nor is it Nothing")
End If

One other shortcut you can take with the String type is use a comparison like the one below to see if the value is either Nothing or the empty string:


Dim tmpString3 As String

If tmpString3 = "" Then
   Console.WriteLine("tmpString3 is set to either Nothing or the empty string")
Else
   Console.WriteLine("tmpString3 is neither the the empty string nor is it Nothing")
End If

Other Built-in Types are Nullable Too!

But what I didn’t realize when I first started writing VB.NET code, is that you can make other built-in data types nullable too.  I accidentally discovered nullable types while using a third party date picker web control that returned a nullable DateTime value (declared “As DateTime?”).

I’m sure you know that this code:


Dim tmpInt1 As Integer

Console.WriteLine("tmpInt1 is " & tmpInt1.ToString)

tmpInt1 = Nothing
Console.WriteLine("after being set to Nothing, tmpInt1 is now " & tmpInt1.ToString)

will produce this output:


tmpInt1 is 0
after being set to Nothing, tmpInt is now 0

But here is where it starts to get VERY interesting.  Take a look at this code:


Dim tmpInt2 As Integer?
If tmpInt2 Is Nothing Then
   Console.WriteLine("tmpInt2 is Nothing")
Else
   Console.WriteLine("tmpInt2 is " & tmpInt2.ToString)
End If

tmpInt2 = 42
If tmpInt2 Is Nothing Then
   Console.WriteLine("after being set, tmpInt2 is now Nothing")
Else
   Console.WriteLine("after being set, tmpInt2 is now " & tmpInt2.ToString)
End If

tmpInt2 = Nothing
If tmpInt2 Is Nothing Then
   Console.WriteLine("after being set to Nothing, tmpInt2 is now Nothing")
Else
   Console.WriteLine("after being set to Nothing, tmpInt2 is now " & tmpInt2.ToString)
End If

and the output it produces:


tmpInt2 is Nothing
after being set, tmpInt2 is now 42
after being set to Nothing, tmpInt2 is now Nothing

What this means is that now you can not only set an Integer value to an value from Integer.MinValue to Integer.MaxValue and 0, you can also set it to Nothing signifying that the value is unknown or not applicable.  Consider you have a configuration setting which is a maximum daily limit for something and you want to configure it to have no limit.  You could set the value to -1 or Integer.MinValue or Integer.MaxValue, but these do not really convey what the real setting is.

Also, imagine if you needed to set much larger limits and you converted the value to a Long.  Now you would have a problem if you had stored the configuration value as Integer.MaxValue, because now you would really need it stored as Long.MaxValue

If you use a nullable Integer, by declaring its type “As Integer?”, to store the value then you can set it to Nothing when there is no limit.  Then, you can store the value in the database as NULL (DBNull.Value).  This give you more consistent, readable code.

So if you are reading from the database:


' Simulate reading a Null value from the database:
Dim DBObject As Object = DBNull.Value
Dim tmpInt2 As Integer?

If IsDBNull(DBObject) Then
   tmpInt2 = Nothing
Else
   tmpInt2 = CInt(DBObject)
End If

If tmpInt2 Is Nothing Then
   Console.WriteLine("after reading value from database, tmpInt2 is now Nothing")
Else
   Console.WriteLine("after reading value from database, tmpInt2 is now " & tmpInt2.ToString)
End If

gives this output:

after reading value from database, tmpInt2 is now Nothing

When you are writing to the database, you can use code similar to this:

Dim DBobject2 As Object
Dim tmpInt3 As Integer? = Nothing

' Simulate setting the value to be written back into the database:
DBobject2 = If(tmpInt3 Is Nothing, DBNull.Value, CInt(tmpInt3))

But be warned, that you can’t use this shortened “If” format when assigning a value out of the database to a nullable type because VB.NET outthinks you and casts the Nothing value to the Integer type of zero:

Dim DBObject As Object = DBNull.Value
Dim tmpInt2 As Integer?

' this type of assignment won't work:
tmpInt2 = If(IsDBNull(DBObject), Nothing, CInt(DBObject))

Conclusion

I hope this introduction to VB.NET nullable types has been helpful.  I have used a nullable Integer in these examples, but when thinking about using them, keep in mind that you can declare all of the built-in types as nullable including Boolean, Decimal, Double.

 

avatar
About Sam (12 Articles)
IT professional and entrepreneur with over 30 years of computer experience. He is a Founder and the Chief Engineer for Tech Savvy Leads, the makers of TSL Ping Tree. In addition to his role at Tech Savvy Leads, he provides senior level technology consulting services.

1 Trackbacks & Pingbacks

  1. Cool VB.NET Features I Wish I Knew on Day One | Tech Savvy Sam

Leave a comment

Your email address will not be published.


*