Click here to Skip to main content
15,896,207 members
Articles / Programming Languages / Visual Basic

NullSafe Functions: Ensuring Safe Variables

Rate me:
Please Sign up or sign in to vote.
3.22/5 (8 votes)
7 Nov 20042 min read 91.9K   113   26  
In this article Rob Walling offers some functions that ensure variables returned from DataSets, ViewState, Session State, or Functions won't break your code.
Public Class Utility

    Public Const WILDCARD_ID As Integer = -1

    '****************************************************************
    ' NullSafeString
    '****************************************************************
    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

    '****************************************************************
    ' NullSafeInteger
    '****************************************************************
    Public Shared Function NullSafeInteger(ByVal arg As Object, Optional ByVal returnIfEmpty As Integer = 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

    '****************************************************************
    '   NullSafeDouble
    '****************************************************************
    Public Shared Function NullSafeDouble(ByVal arg As Object, Optional ByVal returnIfEmpty As Integer = 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

    '****************************************************************
    ' NullSafeBoolean
    '****************************************************************
    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

End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions