Avatar of PhilAI
PhilAI
Flag for United Kingdom of Great Britain and Northern Ireland

asked on 

Has anyone ever created a DBNullable(Of T) structure just like Nullable(Of T)

This is as far as I got with creating such a thing. I have a feeling this is NOT possible, but I would like someone to confirm my theory.

My problem with my below code is that I can define
Dim dbNullableWithInteger As DBNullable(Of Integer) = 5
Dim dbNullableWithNull As DBNullable(Of Integer) = DBNull.Value
Dim nullableWithInteger As Nullable(Of Integer) = 5
Dim nullableWithNull As Nullable(Of Integer) = Nothing

But when I try to retrieve the object it works differently from System.Nullable(Of T):
? nullableWithNull
Nothing
? dbNullableWithNull
{DBNullable(Of Integer)}
    _DBNullable: {DBNullable}
    _HasValue: True
    _Value: 0
    DBNullable: {System.DBNull}
    HasValue: True
    Value: 0
? nullableWithInteger
5
? dbNullableWithInteger
{DBNullable(Of Integer)}
    _DBNullable: {DBNullable}
    _HasValue: True
    _Value: 5
    DBNullable: {System.DBNull}
    HasValue: True
    Value: 5

It returns the Structure and NOT the direct object! Can anyone help master my solution?

Answers in C# or VB.NET are fine - I can write in both, but in this company I am forced to write in VB.NET :o(
Public Class DBNullable
	Inherits System.Object
 
	Private _Value As DBNull
 
	Public Sub New()
		_Value = DBNull.Value
	End Sub
 
	Public Sub New(ByVal value As DBNull)
		_Value = value
	End Sub
 
	Public Shared Widening Operator CType(ByVal value As DBNull) As DBNullable
		Return New DBNullable(value)
	End Operator
 
	Public ReadOnly Property Value() As DBNull
		Get
			Return _Value
		End Get
	End Property
 
	''' <summary>
	''' Returns the underlying type argument of the specified nullable type</summary>
	''' <param name="nullableType">A System.Type object that describes a closed generic nullable type</param>
	''' <returns>The type argument of the nullableType parameter, if the nullableType parameter is a closed generic nullable type; otherwise, null</returns>
	''' <exception cref="System.ArgumentNullException">nullableType is null</exception>
	Public Shared Function GetUnderlyingType(ByVal nullableType As System.Type) As System.Type
		Return Nullable.GetUnderlyingType(nullableType)
	End Function
End Class
 
Public Structure DBNullable(Of T)
	Private _DBNullable As DBNullable
	Private _HasValue As Boolean
	Private _Value As T
 
	''' <summary>
	''' Initializes a new instance of the DBNullable(Of T) structure to the
	''' specified value</summary>
	Public Sub New(ByVal value As DBNull)
		_DBNullable = New DBNullable(value)
		_Value = Me.GetValueOrDefault()
		_HasValue = False
	End Sub
 
	''' <summary>
	''' Initializes a new instance of the DBNullable(Of T) structure to the
	''' specified value</summary>
	''' <param name="value">A value type</param>
	Public Sub New(ByVal value As T)
		_DBNullable = New DBNullable()
		_Value = value
		_HasValue = True
	End Sub
 
	''' <summary>
	''' Widen a DBNull value into a DBNullable(Of T) object</summary>
	''' <param name="value">A DBNull value</param>
	''' <returns>New DBNullable(Of T) structure based on the DBNull value supplied</returns>
	Public Shared Widening Operator CType(ByVal value As DBNull) As DBNullable(Of T)
		Return New DBNullable(Of T)(DBNull.Value)
	End Operator
 
	''' <summary>
	''' Narrow a DBNullable(Of T) object into a DBNull value</summary>
	''' <param name="value">DBNullable(Of T) object</param>
	''' <returns>DBNullable property of the DBNullable(Of T) object</returns>
	Public Shared Narrowing Operator CType(ByVal value As DBNullable(Of T)) As DBNull
		Return value.DBNullable
	End Operator
 
	''' <summary>
	''' Widen a value type into a DBNullable(Of T) object</summary>
	''' <param name="value">A value type</param>
	''' <returns>New DBNullable(Of T) structure based on the value supplied</returns>
	Public Shared Widening Operator CType(ByVal value As T) As DBNullable(Of T)
		Return New DBNullable(Of T)(value)
	End Operator
 
	''' <summary>
	''' Narrow a DBNullable(Of T) object into a value type</summary>
	''' <param name="value">DBNullable(Of T) object</param>
	''' <returns>Value property of the DBNullable(Of T) object</returns>
	Public Shared Narrowing Operator CType(ByVal value As DBNullable(Of T)) As T
		Return value.Value
	End Operator
 
	Public ReadOnly Property DBNullable() As DBNull
		Get
			Return _DBNullable.Value
		End Get
	End Property
 
	''' <summary>
	''' Gets a value indicating whether the current DBNullable(Of T) object
	''' has a value</summary>
	''' <returns>True, if the current DBNullable(Of T) object has a value, otherwise, False</returns>
	Public ReadOnly Property HasValue() As Boolean
		Get
			Return True
		End Get
	End Property
 
	Public ReadOnly Property Value() As T
		Get
			Return _Value
		End Get
	End Property
 
	''' <summary>
	''' Retrieves the value of the current DBNullable(Of T) object, or the
	''' object's default value</summary>
	''' <returns>
	''' The value of the DBNullable(Of T).Value property if the
	''' DBNullable(Of T).HasValue property is true; otherwise, the default value
	''' of the current DBNullable(Of T) object. The type of the default value is
	''' the type argument of the current DBNullable(Of T) object, and the value of
	''' the default value consists solely of binary zeroes</returns>
	Public Function GetValueOrDefault() As T
		Dim defaultValue As T
 
		Return GetValueOrDefault(defaultValue)
	End Function
 
	''' <summary>
	''' Retrieves the value of the current DBNullable(Of T) object, or the
	''' specified default value</summary>
	''' <param name="defaultValue">A value to return if the DBNullable(Of T).HasValue property is false</param>
	''' <returns>
	''' The value of the DBNullable(Of T).Value property if the
	''' DBNullable(Of T).HasValue property is true; otherwise, the defaultValue
	''' parameter</returns>
	Public Function GetValueOrDefault(ByVal defaultValue As T) As T
		If Me.HasValue() Then
			Return defaultValue
		Else
			Return Me.Value
		End If
	End Function
 
	''' <summary>
	''' Returns the text representation of the value of the current
	''' DBNullable(Of T) object</summary>
	''' <returns>
	''' The text representation of the value of the current DBNullable(Of T)
	''' object if the DBNullable(Of T).HasValue property is true, or an empty
	''' string ("") if the DBNullable(Of T).HasValue property is false</returns>
	Public Overrides Function ToString() As String
		If Me.HasValue() Then
			Return _Value.ToString()
		Else
			Return String.Empty
		End If
	End Function
End Structure

Open in new window

.NET ProgrammingVisual Basic.NET

Avatar of undefined
Last Comment
Bob Learned

8/22/2022 - Mon