Results 1 to 4 of 4

Thread: anything wrong with using datatype object and determine datatype in class property?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2008
    Posts
    119

    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

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. Private _custID As Integer?
    2.  
    3. Public Property CustID() As Integer?
    4.     Get
    5.         Return Me._custID
    6.     End Get
    7.     Set(ByVal value As Integer?)
    8.         Me._custID = value
    9.     End Set
    10. End Property
    Your code to transfer the value from the DataSet should look like this:
    vb.net Code:
    1. If myRow.IsCustIDNull() Then
    2.     myObject.CustID = Nothing
    3. Else
    4.     myObject.CustID = myRow.CustID
    5. End If
    Your code to transfer the value to the DataSet should look like this:
    vb.net Code:
    1. If myObject.CustID.HasValue Then
    2.     myRow.CustID = myObject.CustID.Value
    3. Else
    4.     myRow.SetCustIDNull()
    5. End If
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Hyperactive Member The Fire Snake's Avatar
    Join Date
    Sep 2009
    Location
    USA
    Posts
    401

    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.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Aug 2008
    Posts
    119

    Re: anything wrong with using datatype object and determine datatype in class propert

    thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width