|
-
Mar 9th, 2010, 12:07 AM
#1
Thread Starter
Lively Member
anything wrong with using datatype object and determine datatype in class property?
anything wrong with using datatype object and determine datatype in class property?
I have a typed dataset and it has several integers columns that are null in the db. and when I make a call it throws exception..
is there anythign wrong with setting it as object in my dataset.. changing the throw exception property to Nothing and passing it to my property as integer?
Code:
for each row in myDS.DataTable
cRate.CustID = row.CustID
next row
and in my class
Code:
Public Property CustID() As Integer
Get
Return _custID
End Get
Set(ByVal value As Integer)
_custID = value
End Set
End Property
-
Mar 9th, 2010, 12:36 AM
#2
Re: anything wrong with using datatype object and determine datatype in class propert
When you create a typed DataSet, your typed DataRow classes are given a property for each column in the corresponding table. If a column accepts nulls, two helper methods are also generated. If you have a nullable column named CustID of type int, the DataRow will have a property named CustID and methods named IsCustIDNull and SetCustIDNull.
In your .NET classes, if you want to represent a nullable Integer then you should not use the Integer type but rather the Integer? type, which is shorthand for Nullable(Of Integer). Your own class property would look like this:
vb.net Code:
Private _custID As Integer? Public Property CustID() As Integer? Get Return Me._custID End Get Set(ByVal value As Integer?) Me._custID = value End Set End Property
Your code to transfer the value from the DataSet should look like this:
vb.net Code:
If myRow.IsCustIDNull() Then myObject.CustID = Nothing Else myObject.CustID = myRow.CustID End If
Your code to transfer the value to the DataSet should look like this:
vb.net Code:
If myObject.CustID.HasValue Then myRow.CustID = myObject.CustID.Value Else myRow.SetCustIDNull() End If
-
Mar 9th, 2010, 12:37 AM
#3
Hyperactive Member
Re: anything wrong with using datatype object and determine datatype in class propert
What specifically are you trying to do? I don't understand when you say "I have a typed dataset and it has several integers columns that are null in the db. and when I make a call it throws exception.." You could check to see if the value coming back from the database is a null by checking for DBNull.value and then take some action based on that.
In general you can set an object to be of type object but beware of late binding which is inefficient, as the compiler has to determine the type of your object at run time and cannot apply the code optimizations and checks at compile time.
-
Mar 9th, 2010, 01:05 AM
#4
Thread Starter
Lively Member
Re: anything wrong with using datatype object and determine datatype in class propert
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|