473,385 Members | 1,320 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Common Problem, I think...

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

Nov 21 '05 #1
5 2350
Chad,

I'm pretty sure that Visual Studio 2005 will add support for nullable value
types.

If this were an important feature for me I would use VS2005 instead of
trying to work around this limitation in current versions of Visual Studio.

Kerry Moorman

"Chad" wrote:
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

Nov 21 '05 #2
Hi,

I recommend that you study, study, study. You are very unfamiliar with what you are talking about. But let's get you going. First place the keyword "Default" in front of your Value property and add the argument it requires. This will give you the default property you are looking for. I'm not sure why you thought that wasn't available. Next study the DataReader object. GetSqlInt32 has nothing to do with conversion. The number you are giving it is the index of the column that should already hold an Integer you retrieved through a query. The Integer datatype in all versions of VB.Net has always been a 32bit signed integer. You should have no problem using CInt to convert whatever value you get from a DataReader or DataAdapter. For NULL I would recommend exposing an IsNull property that is boolean. Then test for it before performing operations. That should both reduce the amount of code you need to write by testing after the fact and is intuitive. Or you can create your own function like .NET's IsDBNull(), but it isn't as elegant. First things first though. Study the framework and dig into the object model. 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...
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

Nov 21 '05 #3
Chad,

In addition to Ken

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?

Cor

Nov 21 '05 #4

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
Nov 21 '05 #5
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
Nov 21 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: S.W. Rasmussen | last post by:
With the risk of being accused of multi-posting I would like to draw the attention to a serious visual basic/windows issue discussed in the microsoft.public.vb.bugs newsgroup. As pointed out below...
8
by: John M. Gabriele | last post by:
I'm putting together a small site using Python and cgi. (I'm pretty new to this, but I've worked a little with JSP/servlets/Java before.) Almost all pages on the site will share some common...
5
by: wrecker | last post by:
Hi all, I have a few common methods that I need to use at different points in my web application. I'm wondering where the best place would be to put these? I think that I have three options. ...
0
by: Bo Gusman | last post by:
A curious problem, one that I think I've seen before. I have a .250 MLOC vb6 app that uses the common dialog control. If I select File/Open in my app to display the file open dialog and then...
1
by: kmslick | last post by:
Hello all. Not sure which group this problem best relates to, so I'm posting to both with a follow up. I started learning C# and .NET last august for a project for my employer. The project...
22
by: David Mathog | last post by:
One thing that keeps coming up in this forum is that standard C lacks many functions which are required in a workstation or server but not possible in an embedded controller. This results in a...
3
by: DaBrain | last post by:
Many thank to anyone who has the time to read an reply to this. Am a lone gun developer, I do not develop in team so this question comes from a place where I just don't really KNOW what others...
6
by: Wayne Shu | last post by:
hi everyone! I have a problem in implementing a common class interface. my assignment is to implement a data structure list, and I have define a class template list_base, it's an abstract class,...
4
by: | last post by:
I have learned about compartmentalizing my code base using Class Libraries. I have my common code such as my ORM framework broken out into their own Class Libraries, which are referenced as...
8
by: Arno R | last post by:
Hi all. When I need to search for pictures, I always have too choose thumbnail-view manually. Is it possible to open the common dialog in thumbnail-view programmatically? Example ?? At the...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.