C
Chad
I want to create a class which contains a date, integer and string property which all allow a NULL value.
I don't want to store the values in an Object datatype. I prefer more strongly typed data.
I am interested in using SQL data types for my property types, but in our 4-tier app, we have a Data access layer that passes dataset across tiers to the Application layer, which populates the Business object's properties. In the Data layer, we do everything via Stored procedures using SQLData Adapters.
My understanding is that I would need to use the SQLDataReader.GetSqlInt32 and similar methods to covert a normal integer to the SqlInt32 type before I could store a value in the SqlInt32 property, apparently the CTYPE will not work.
If I have a property "MyClass.MyInt32Property" how would I move a constant integer value iof "2" to this property from the GUI layer which normally doesn't have access to database related classes? Apparently, before I use the DataReader class, I must have an open connection as query results via a Command object.
I consider created my own class which mimics the Integer class but allows for NULL values. Too bad I can't seem to inherit from the Integer base class. However, this class is a tad awkward since now I have to refer to its value using the Value Property and in .NET there is no longer a concept of a default property..
Suggestions would be appreciated.
Option Strict On
Option Explicit On
#Region "IntegerNullable Class-Replacement for Integer datatype. Accepts an Integer, DBNULL, or Nothing"
Public Class IntegerNullable: Inherits System.Object
Implements System.Data.SqlTypes.INullable
Private Const mstrReplaceDataType As String = "System.Int32"
Private mobjValue As Object
Public Property Value() As Object
Get
Return mobjValue
End Get
Set(ByVal Value As Object)
If Value Is Nothing Or Value Is System.DBNull.Value Then
mobjValue = System.DBNull.Value
ElseIf Value.GetType.Name.ToUpper = mstrReplaceDataType.ToUpper Then
mobjValue = Value
Else
Throw New Exception(Me.GetType.Name & " type only accepts values of type " & mstrReplaceDataType & ", DBNull, and Nothing")
End If
End Set
End Property
Public Function ToInteger() As Integer
If IsNull Then
Throw New Exception("Value of " & Me.GetType.Name & " object is Nothing or is DBNull. ToInteger method fails")
End If
Return CType(mobjValue, Integer)
End Function
Public ReadOnly Property IsNull() As Boolean Implements System.Data.SqlTypes.INullable.IsNull
Get
Return (mobjValue Is Nothing Or mobjValue Is System.DBNull.Value)
End Get
End Property
End Class
#End Region
I don't want to store the values in an Object datatype. I prefer more strongly typed data.
I am interested in using SQL data types for my property types, but in our 4-tier app, we have a Data access layer that passes dataset across tiers to the Application layer, which populates the Business object's properties. In the Data layer, we do everything via Stored procedures using SQLData Adapters.
My understanding is that I would need to use the SQLDataReader.GetSqlInt32 and similar methods to covert a normal integer to the SqlInt32 type before I could store a value in the SqlInt32 property, apparently the CTYPE will not work.
If I have a property "MyClass.MyInt32Property" how would I move a constant integer value iof "2" to this property from the GUI layer which normally doesn't have access to database related classes? Apparently, before I use the DataReader class, I must have an open connection as query results via a Command object.
I consider created my own class which mimics the Integer class but allows for NULL values. Too bad I can't seem to inherit from the Integer base class. However, this class is a tad awkward since now I have to refer to its value using the Value Property and in .NET there is no longer a concept of a default property..
Suggestions would be appreciated.
Option Strict On
Option Explicit On
#Region "IntegerNullable Class-Replacement for Integer datatype. Accepts an Integer, DBNULL, or Nothing"
Public Class IntegerNullable: Inherits System.Object
Implements System.Data.SqlTypes.INullable
Private Const mstrReplaceDataType As String = "System.Int32"
Private mobjValue As Object
Public Property Value() As Object
Get
Return mobjValue
End Get
Set(ByVal Value As Object)
If Value Is Nothing Or Value Is System.DBNull.Value Then
mobjValue = System.DBNull.Value
ElseIf Value.GetType.Name.ToUpper = mstrReplaceDataType.ToUpper Then
mobjValue = Value
Else
Throw New Exception(Me.GetType.Name & " type only accepts values of type " & mstrReplaceDataType & ", DBNull, and Nothing")
End If
End Set
End Property
Public Function ToInteger() As Integer
If IsNull Then
Throw New Exception("Value of " & Me.GetType.Name & " object is Nothing or is DBNull. ToInteger method fails")
End If
Return CType(mobjValue, Integer)
End Function
Public ReadOnly Property IsNull() As Boolean Implements System.Data.SqlTypes.INullable.IsNull
Get
Return (mobjValue Is Nothing Or mobjValue Is System.DBNull.Value)
End Get
End Property
End Class
#End Region