Introduction
Values retrieved from DataSet
s, ViewState, Session State, or any number of other storage mechanisms can cause errors if they comes back as DBNull
or Nothing
. As an example, let's say you want to retrieve a String
from a DataSet
. Typically, you begin by checking if the value is DBNull.Value
, then casting the value to a String
(if you code with Option Strict On
), then checking if it's Nothing
, then trimming it. A similar process applies when retrieving a String
from ViewState and Session State:
Dim firstName as string
If ds.Tables(0).Rows("FirstName") is DBNull.Value Then
firstName = String.Empty
Else
firstName = CStr(ds.Tables(0).Rows("FirstName"))
If Not IsNothing(firstName) Then
firstName = firstName.Trim
End If
End If
To overcome this cumbersome and repetitive process, I've created several "NullSafe" functions that handle the casting, trimming, and safety-checking of values.
Making Strings NullSafe
The first function is called NullSafeString
. Notice that if the arg
is DBNull
, Nothing
or String.Empty
, the function defaults to returning an empty string, but you can override this behavior by passing a String
of your choice as the second parameter:
Public Shared Function NullSafeString(ByVal arg As Object, _
Optional ByVal returnIfEmpty As String = "") As String
Dim returnValue As String
If (arg Is DBNull.Value) OrElse (arg Is Nothing) _
OrElse (arg Is String.Empty) Then
returnValue = returnIfEmpty
Else
Try
returnValue = CStr(arg).Trim
Catch
returnValue = returnIfEmpty
End Try
End If
Return returnValue
End Function
Using the above function which resides in my Utility
class, the code to pull FirstName
from the DataSet
becomes:
Dim firstName as string = _
Utility.NullSafeString(ds.Tables(0).Rows("FirstName"), "Not Available")
This function guarantees that the value of firstName
is safe to work with. In other words, method calls like firstName.Length
, firstName.Replace
will not throw exceptions.
Integers, Doubles, and Booleans
To ensure a value is strongly-typed upon its return from the NullSafe function, I create a new method for each data type. On most projects, I have four NullSafe methods, but on a few, I've had to extend to six to encompass Long
s and Short
s. Below are my NullSafeInteger
, NullSafeDouble
, and NullSafeBoolean
methods. You'll notice the default return value for the numeric functions is WILDCARD_ID
, a constant I create with a value of -1. In the calling code, I can then check for this constant value to determine if the value is "valid."
Public Shared Function NullSafeInteger(ByVal arg As Object, _
Optional ByVal returnIfEmpty As Integer = Constants.WILDCARD_ID) As Integer
Dim returnValue As Integer
If (arg Is DBNull.Value) OrElse (arg Is Nothing) _
OrElse (arg Is String.Empty) Then
returnValue = returnIfEmpty
Else
Try
returnValue = CInt(arg)
Catch
returnValue = returnIfEmpty
End Try
End If
Return returnValue
End Function
Public Shared Function NullSafeDouble(ByVal arg As Object, _
Optional ByVal returnIfEmpty As Integer = Constants.WILDCARD_ID) As Double
Dim returnValue As Double
If (arg Is DBNull.Value) OrElse (arg Is Nothing) _
OrElse (arg Is String.Empty) Then
returnValue = returnIfEmpty
Else
Try
returnValue = CDbl(arg)
Catch
returnValue = returnIfEmpty
End Try
End If
Return returnValue
End Function
Public Shared Function NullSafeBoolean(ByVal arg As Object) As Boolean
Dim returnValue As Boolean
If (arg Is DBNull.Value) OrElse (arg Is Nothing) _
OrElse (arg Is String.Empty) Then
returnValue = False
Else
Try
returnValue = CBool(arg)
Catch
returnValue = False
End Try
End If
Return returnValue
End Function
Conclusion
There is nothing magical about these functions; they simply encapsulate repetitive and cumbersome code into easy to use functions. Best of luck putting them to use!
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.