Hi Chad,
I would just use the DataReader.GetInt32() function instead of the GetSqlInt32() function. Unless there is something very specific you need out of it. If there is something very specific then change this line:
Account.SupervisorId = CInt(2)
To:
Account.SupervisorId = New SqlInt32(2)
That will allow you to assign a regular integer to a SQL type integer. Note that if you are using these things in large loops I would definitely move to native .Net types like Integer as they are a little over twice as fast. When I build my objects I use native .Net types and when I read from a dataread I just use GetInt32(). You can even use CInt(DataReader("ColumnName")) then also. Good luck! Ken.
--
Ken Dopierala Jr.
For great ASP.Net web hosting try:
http://www.webhost4life.com/default.asp?refid=Spinlight
If you sign up under me and need help, email me.
"Chad" <ch**************@unisys.com> wrote in message news:dc**********@trsvr.tr.unisys.com...
When I said that "the Value property on my object cannot be made the default property" I meant that in VB.NET, unlike VB6, you could not have a default property unless the property accepts a parameter.
In the IntergerNullable class I created, the Value property accepted no params. I believe you suggested that it should. I am confused as to what you are trying to accomplish by adding a param.
Secondly, I see you are correct about the SQLDataReader.GetSqlInt32 method, it does accept as its param, the index of the field that you want to get as a SqlInt32 datatype. However, I feel my question remains.
Please consider the example below. I have a class Account, which has three properties, AccountId, AccountMgrId and SupervisorId. The first is a required integer property. The other properties I would like to allow a NULL value or an integer value only. The AccountMgrId property is implemented to use my custom class IntegerNullable, which is a class that I created to "extend" the Integer so it can also accept NULL values. The problem I have with it is that, because unlike the native INTEGER type, it is a true object and it is awkward to refer to the value of an IntegerNullable type by refering to its Value property rather than just the object itself. However, I do not consider this horrible. In fact, I kind of think that Microsft maybe should have implemented Integers as a real object for consistancy, but I suspect there is alot to this subject. Anyway...
The SupervisorID I implemented in the Account class as a SqlInt32 datatype. I can move a value from the SupervisorId database column to this property using this command:
Account.SupervisorId = DataReader.GetSqlInt32(1)
but in the GUI layer, how do I move a value, say the Integer value "2" to the Account.Supervisor property?
As you see below, when I attempt to do this, I get the error 'System.DBNull' cannot be converted to 'System.Data.SqlTypes.SqlInt32'.
I get the same error if I try to use the CTYPE or the CINT functions:
Account.SupervisorId = CInt(2) 'Value of type 'Integer' cannot be converted to 'System.Data.SqlTypes.SqlInt32'.
Account.SupervisorId = CType(2, Integer) 'Value of type 'Integer' cannot be converted to 'System.Data.SqlTypes.SqlInt32'.
Cor wrote:
A value is placed on the stack and holds (a) value(s)
An object is placed on the managed heap. If its address to that is null, than it Is Nothing or as you wrote (Null).
I hope that this gives an idea?
That wasn't enough help for me. Perhaps you can clarify this point for me?
Anyway, I think my IntegerNullable class will work fine, I just think it is a little awkward.
You comments are welcomed.
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim Account As New Account(AccountId:=1)
Dim i As Integer
Account.AccountMgrId = New IntegerNullable(2)
If Account.AccountMgrId.IsNull Then
System.Diagnostics.Debug.WriteLine("The Account has no Manager")
Else
System.Diagnostics.Debug.WriteLine("The Manager's ID is " & Account.AccountMgrId.Value)
End If
Dim DataReader As SqlDataReader
Account.SupervisorId = DBNull.Value 'System.DBNull' cannot be converted to 'System.Data.SqlTypes.SqlInt32'.
Account.SupervisorId = 2 'System.DBNull' cannot be converted to 'System.Data.SqlTypes.SqlInt32'.
End Sub
Public Class Account
'An account must have an ID but it may or may not have an Account Manager
Private m_AccountID As Integer
Private m_AccountMgrId As IntegerNullable
Private m_SupervisorId As System.Data.SqlTypes.SqlInt32
Public Sub New(ByVal AccountID As Integer)
m_AccountID = AccountID
m_AccountMgrId = New IntegerNullable
End Sub
Public Property AccountId() As Integer
Get
Return m_AccountID
End Get
Set(ByVal AccountId As Integer)
m_AccountID = AccountId
End Set
End Property
Public Property AccountMgrId() As IntegerNullable
Get
Return m_AccountMgrId
End Get
Set(ByVal AccountMgrId As IntegerNullable)
m_AccountMgrId = AccountMgrId
End Set
End Property
Public Property SupervisorId() As System.Data.SqlTypes.SqlInt32
Get
Return m_SupervisorId
End Get
Set(ByVal Value As System.Data.SqlTypes.SqlInt32)
m_SupervisorId = Value
End Set
End Property
End Class
Option Strict On
Option Explicit On
Public Class IntegerNullable
Inherits System.Object
Implements System.Data.SqlTypes.INullable
Private mintValue As Integer
Private mblnIsNull As Boolean
Public Sub New()
mintValue = Nothing
mblnIsNull = True
End Sub
Public Sub New(ByVal Value As Integer)
mintValue = Value
mblnIsNull = True
End Sub
Public Property Value() As Integer
Get
If mblnIsNull Then
Throw New Exception("Value property is not accessible when isNull is True. Use IsNull property to test before accessing Value property")
End If
Return mintValue
End Get
Set(ByVal Value As Integer)
mintValue = Value
mblnIsNull = False
End Set
End Property
Public ReadOnly Property IsNull() As Boolean Implements System.Data.SqlTypes.INullable.IsNull
Get
Return mblnIsNull
End Get
End Property
Public Sub Nullify()
mblnIsNull = True
mintValue = Nothing
End Sub
End Class