Database.NET Tip: Using a Nullable Value Type

.NET Tip: Using a Nullable Value Type

Developer.com content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

How often do your programs grab some data from a database and throw the values returned into local variables without giving the data another thought? I used to, until my programs started throwing exceptions because of nulls in the data. So, what did I do? I checked every field coming back from the database that could possibly be null before storing it in a local variable. Depending upon what your database allows, this could lead to an explosion of code. Here is an example of the type of code that was used to check for a null value before storing it in a local variable. In this case, the BranchCode from a DataReader is being checked:

double BranchCode;
if (dr["BranchCode"] == DBNull.Value)
   BranchCode = -1;
else
    BranchCode = dr["BranchCode"];

This worked, but later in the code the “magic value” of -1 would have to be checked to see whether the variable had a valid BranchCode.

if (BranchCode <> -1)
{
   ...
}

Nullable types allow a cleaner solution to this problem. You can store the data returned from the database into a nullable variable without worry about an exception being thrown at that time. You still may have to perform the check for a valid value later, depending upon exactly how you are using the variable. You can declare a value type variable as nullable using the ? type modifier. Nullable types have two read-only properties that you use to get information about the variable. HasValue returns false if the value is null; otherwise, it returns true. If HasValue returns true, you can access the Value property, which contains the currently stored value. If you attempt to use the Value property when HasValue is false, an exception will be thrown. Here is what the example above would look like using a nullable type:

double? BranchCode;
BranchCode = dr["BranchCode"];

if (BranchCode.HasValue)
{
   switch (BranchCode.Value)
   {
      ...
   }
}

In addition to checking HasValue, you can also just compare the value to null, like this:

if (BranchCode != null)
{
   ...
}

You do need to use some caution, however, when using nullable types. Nullable numeric types can be used in comparisons and expressions just like normal numeric expressions. If the nullable variable has a non-null value, everything will work as you would expect. If the nullable variable is null, you need to realize that the result of the expression could be null and make sure that case is handled appropriately.

About the Author

Jay Miller is a Software Engineer with Electronic Tracking Systems, a company dedicated to robbery prevention, apprehension, and recovery based in Carrollton, Texas. Jay has been working with .NET since the release of the first beta and is co-author of Learn Microsoft Visual Basic.Net In a Weekend. Jay can be reached via email at jmiller@sm-ets.com.

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories