Archive

Posts Tagged ‘Nothing’

Working with null values in the .NET Framework

June 17th, 2009 1 comment

One of the trickier aspects of application development is dealing with null or nonexistent data. It gets complicated when the application expects a data value and gets nothing or unexpected values; that’s when you must perform coding that properly handles these situations to avoid application failure. This requires a close examination of the data wherever and whenever it’s needed. Let’s take a closer look at working with null values within the .NET Framework.

Null and void

A null value is a value that doesn’t refer to any object. It’s the default value of reference-type variables (e.g., class, interface, delegate).

These items are called reference types because they’re objects that store references to the actual data. String, object, and struct are reference type examples. The null keyword is a literal that represents a null reference with the C# .NET environment. You may use the null keyword to check or assign the value of an object. For example, the following C# code determines if a string value is null:

string sTest = “Test”;
if (sTest == null) {
Console.WriteLine(“sTest is Null”)
}

This code works without any problems with C#, but there’s no VB.NET equivalent of the null keyword. Instead, VB.NET uses the Nothing keyword. The following code demonstrates its use:

Dim sTest As String
If (sTest Is Nothing) Then
Console.WriteLine(“sTest is Null”)
End If

Another area of confusion stems from the fact that VB.NET doesn’t treat null and Nothing as equals; consequently, a VB.NET programmer may have to check for both values.

The variation in support may cause confusion, but a developer rarely develops in both languages simultaneously. On the other hand, Microsoft provides a uniform method for working with null values: The base System.Convert namespace includes the DBNull object.

DBNull

The use of the DBNull class indicates the absence of a known value. While the Microsoft documentation says it’s typically in a database application, you may also use it with any type of data.

In database applications, a null object is a valid value for a field. This class differentiates between a null value (a null object) and an uninitialized value (the DBNull.Value instance). For example, a table can have records with uninitialized fields. By default, these uninitialized fields have the DBNull value.

You can utilize the DBNull object by way of its Value property. This represents a null value, which is never equal to anything. The following C# snippet revises the previous example to utilize the DBNull class:

if (sTest.Equals(System.DBNull.Value)) {
Console.WriteLine(“sTest is null.”);

Notice how this code utilizes the string class’s equality method. The VB.NET sample may utilize the same code, like this:

If (sTest.Equals(System.DBNull.Value) Then
Console.WriteLine(“sTest is Null”)
End  If

You may also use the DBNull.Value constant on the right-hand of an assignment operation, thus storing a null value within a reference type. In addition, the .NET Framework provides the IsDBNull function (in the System.Convert namespace), which is a shorthand approach for determining if a reference type contains a null value. The following VB.NET uses IsDBNull to check an object’s value:

Dim oTest As Object = DBNull.Value
If ((IsDBNull(oTest))) Then
Console.WriteLine(“oTest is Null”)
End If

Even if you check for null values at every conceivable location within your code, they still may creep into the application. Fortunately, the try/catch exception block provides another line of defense; it allows you to catch any unhandled exceptions that null values cause. The System namespace provides the NullReferenceException for these situations. See how the following C# code uses NullReferenceException.

try {
if (sTest.Equals(System.DBNull.Value)) {
Console.WriteLine(“sTest is null.”);
}

} catch (System.NullReferenceException e) {
Console.WriteLine(“Error: ” + e.ToString());
}

Handle your data with care

Applications don’t always receive the type of data they expect; for instance, an app may be looking for integer values and receive string or null values. Therefore, since data is the backbone of an application, you must be extremely careful when working with it.

Categories: Development, SQL, VB .Net Tags: ,