' Nullify Class ' http://www.thoughtproject.com/Snippets/Nullify/ Option Explicit On Option Strict On ''' ''' Converts values to DBNull.Value when null or equal to a value treated as null. ''' ''' ''' ''' Each static method examines a value and either returns DBNull.Value or ''' the value itself. Most methods have overloads for value types and ''' nullable types; parameter ordering and naming conventions are as ''' consistent as possible. For example, each method is named after the ''' data type as it appears in the System namespace. ''' ''' ''' Originally from http://www.thoughtproject.com/Snippets/Nullify and ''' placed into the public domain by the author. Check the above site ''' for new versions. ''' ''' Public NotInheritable Class Nullify Private Sub New() End Sub ''' ''' Returns DBNull.Value if a nullable Boolean is null. ''' Public Shared Function [Boolean](ByVal value As Nullable(Of Boolean)) As Object If value.HasValue Then Return value.Value Else Return DBNull.Value End If End Function ''' ''' Returns DBNull.Value if a nullable Boolean is null or matches a specified value. ''' Public Shared Function [Boolean]( _ ByVal value As Nullable(Of Boolean), _ ByVal treatedAsNull As Boolean) As Object If Not value.HasValue OrElse value.Value = treatedAsNull Then Return DBNull.Value Else Return value.Value End If End Function ''' ''' Returns DBNull.Value if a byte array is null. ''' Public Shared Function ByteArray(ByVal value As Byte()) As Object If value Is Nothing Then Return DBNull.Value Else Return value End If End Function ''' ''' Returns DBNull.Value if a DateTime is the minimum possible date. ''' Public Shared Function DateTime(ByVal value As DateTime) As Object If value = System.DateTime.MinValue Then Return DBNull.Value Else Return value End If End Function ''' ''' Returns DBNull.Value if a DateTime matches a reserved value. ''' Public Shared Function DateTime( _ ByVal value As DateTime, _ ByVal treatedAsNull As DateTime) As Object If value = treatedAsNull Then Return DBNull.Value Else Return value End If End Function ''' ''' Returns DBNull.Value if a DateTime is null. ''' Public Shared Function DateTime(ByVal value As Nullable(Of DateTime)) As Object If value.HasValue Then Return value.Value Else Return DBNull.Value End If End Function ''' ''' Returns DBNull.Value if a DateTime is null or matches a reserved value. ''' Public Shared Function DateTime( _ ByVal value As Nullable(Of DateTime), _ ByVal treatedAsNull As DateTime) As Object If Not value.HasValue OrElse value.Value = treatedAsNull Then Return DBNull.Value Else Return value.Value End If End Function ''' ''' Returns DBNull.Value if a decimal is zero. ''' Public Shared Function [Decimal](ByVal value As Decimal) As Object If value = 0 Then Return DBNull.Value Else Return value End If End Function ''' ''' Returns DBNull.Value if a decimal matches a reserved value. ''' Public Shared Function [Decimal]( _ ByVal value As Decimal, _ ByVal treatedAsNull As Decimal) As Object If value = treatedAsNull Then Return DBNull.Value Else Return value End If End Function ''' ''' Returns DBNull.Value if a nullable Decimal is null. ''' ''' ''' ''' Public Shared Function [Decimal](ByVal value As Nullable(Of Decimal)) As Object If value.HasValue Then Return value.Value Else Return DBNull.Value End If End Function ''' ''' Returns DBNull.Value if a Decimal is null or matches a reserved value. ''' ''' ''' ''' ''' Public Shared Function [Decimal]( _ ByVal value As Nullable(Of Decimal), _ ByVal treatedAsNull As Decimal) As Object If Not value.HasValue OrElse value.Value = treatedAsNull Then Return DBNull.Value Else Return value.Value End If End Function ''' ''' Returns DBNull.Value if a Double is 0.0. ''' Public Shared Function [Double](ByVal value As Double) As Object If value = 0.0 Then Return DBNull.Value Else Return value End If End Function ''' ''' Returns DBNull.Value if a Double matches a reserved value. ''' Public Shared Function [Double]( _ ByVal value As Double, _ ByVal treatedAsNull As Double) As Object If value = treatedAsNull Then Return DBNull.Value Else Return value End If End Function ''' ''' Returns DBNull.Value if a nullable Double is null. ''' ''' ''' ''' Public Shared Function [Double](ByVal value As Nullable(Of Double)) As Object If value.HasValue Then Return value.Value Else Return DBNull.Value End If End Function ''' ''' Returns DBNull.Value if a Double is null or matches a reserved value. ''' Public Shared Function [Double]( _ ByVal value As Nullable(Of Double), _ ByVal treatedAsNull As Double) As Object If Not value.HasValue OrElse value.Value = treatedAsNull Then Return DBNull.Value Else Return value.Value End If End Function ''' ''' Returns DBNull.Value if a Guid is empty. ''' Public Shared Function Guid(ByVal value As Guid) As Object If value = System.Guid.Empty Then Return DBNull.Value Else Return value End If End Function ''' ''' Returns DBNull.Value if a Guid matches a reserved value. ''' Public Shared Function Guid( _ ByVal value As Guid, _ ByVal treatedAsNull As Guid) As Object If value = treatedAsNull Then Return DBNull.Value Else Return value End If End Function ''' ''' Returns DBNull.Value if a Guid is null. ''' Public Shared Function Guid(ByVal value As Nullable(Of Guid)) As Object If value.HasValue Then Return value.Value Else Return DBNull.Value End If End Function ''' ''' Returns DBNull.Value if a nullable Guid is null or matches a reserved value. ''' Public Shared Function Guid( _ ByVal value As Nullable(Of Guid), _ ByVal treatedAsNull As Guid) As Object If Not value.HasValue OrElse value.Value = treatedAsNull Then Return DBNull.Value Else Return value.Value End If End Function ''' ''' Returns DBNull.Value if an Int16 is zero. ''' Public Shared Function Int16(ByVal value As Int16) As Object If value = 0 Then Return DBNull.Value Else Return value End If End Function ''' ''' Returns DBNull.Value if an Int16 matches a reserved value. ''' Public Shared Function Int16( _ ByVal value As Int16, _ ByVal treatedAsNull As Int16) As Object If value = treatedAsNull Then Return DBNull.Value Else Return value End If End Function ''' ''' Returns DBNull.Value if an Int16 is null. ''' Public Shared Function Int16(ByVal value As Nullable(Of Int16)) As Object If value.HasValue Then Return value.Value Else Return DBNull.Value End If End Function ''' ''' Returns DBNull.Value if an Int16 is null or matches a reserved value. ''' Public Shared Function Int16( _ ByVal value As Nullable(Of Int16), _ ByVal treatedAsNull As Int16) As Object If Not value.HasValue OrElse value.Value = treatedAsNull Then Return DBNull.Value Else Return value.Value End If End Function ''' ''' Returns DBNull.Value if an Int32 is zero. ''' Public Shared Function Int32(ByVal value As Int32) As Object If value = 0 Then Return DBNull.Value Else Return value End If End Function ''' ''' Returns DBNull.Value if an Int32 matches a reserved value. ''' Public Shared Function Int32( _ ByVal value As Int32, _ ByVal treatedAsNull As Int32) As Object If value = treatedAsNull Then Return DBNull.Value Else Return value End If End Function ''' ''' Returns DBNull.Value if an Int32 is null. ''' Public Shared Function Int32(ByVal value As Nullable(Of Int32)) As Object If value.HasValue Then Return value.Value Else Return DBNull.Value End If End Function ''' ''' Returns DBNull.Value if an Int32 is null or matches a reserved value. ''' Public Shared Function Int32( _ ByVal value As Nullable(Of Int32), _ ByVal treatedAsNull As Int32) As Object If Not value.HasValue OrElse value.Value = treatedAsNull Then Return DBNull.Value Else Return value.Value End If End Function ''' ''' Returns DBNull.Value if an Int64 is zero. ''' Public Shared Function Int64(ByVal value As Int64) As Object If value = 0 Then Return DBNull.Value Else Return value End If End Function ''' ''' Returns DBNull.Value if an Int64 matches a reserved value. ''' Public Shared Function Int64( _ ByVal value As Int64, _ ByVal treatedAsNull As Int64) As Object If value = treatedAsNull Then Return DBNull.Value Else Return value End If End Function ''' ''' Returns DBNull.Value if an Int64 is null. ''' Public Shared Function Int64(ByVal value As Nullable(Of Int64)) As Object If value.HasValue Then Return value.Value Else Return DBNull.Value End If End Function ''' ''' Returns DBNull.Value if an Int64 is null or matches a reserved value. ''' Public Shared Function Int64( _ ByVal value As Nullable(Of Int64), _ ByVal treatedAsNull As Int64) As Object If Not value.HasValue OrElse value.Value = treatedAsNull Then Return DBNull.Value Else Return value.Value End If End Function ''' ''' Returns DBNull.Value if a Single is zero. ''' Public Shared Function [Single](ByVal value As Single) As Object If value = 0.0 Then Return DBNull.Value Else Return value End If End Function ''' ''' Returns DBNull.Value if a Single matches a reserved value. ''' Public Shared Function [Single]( _ ByVal value As Single, _ ByVal treatedAsNull As Single) As Object If value = treatedAsNull Then Return DBNull.Value Else Return value End If End Function ''' ''' Returns DBNull.Value if a Single is null. ''' Public Shared Function [Single](ByVal value As Nullable(Of Single)) As Object If value.HasValue Then Return value.Value Else Return DBNull.Value End If End Function ''' ''' Returns DBNull.Value if a Single is null or matches a reserved value. ''' Public Shared Function [Single]( _ ByVal value As Nullable(Of Single), _ ByVal treatedAsNull As Single) As Object If Not value.HasValue OrElse value.Value = treatedAsNull Then Return DBNull.Value Else Return value.Value End If End Function ''' ''' Returns DBNull.Value if a String is null or empty. ''' Public Shared Function [String](ByVal value As String) As Object If String.IsNullOrEmpty(value) Then Return DBNull.Value Else Return value End If End Function End Class