|
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.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.