Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Create Class object for specific DB Record?

Status
Not open for further replies.

CraigBest

Programmer
Aug 1, 2001
545
US
OK, Sorwen told me to think about how classes might be useful in my app even though I've seriously cut back on my plans to use them. So I'm thinking of new and better ways to use the classes I've already designed for my railroad program.

As I'm making progress on some of the routines that actually work with the data, it seems to me a good use of a class would be to instantiate it and read in a specific record from a data table that I need to process on. For instance, I have some processing to do involving a specific train. If I load the train record from the DB into an instance of the Train Class I made, I easily have access to the fields as properties, and could create methods that would, for instance, Update the Location property of the train in the DB by passing in the new location variable.

Since I will need to refer to the Train information several times during this process, it seems to make sense to me to do it this way rather than creating a bunch of local variables and reading the record into them field by field. Right?

If that's so, how can I create a class object based on the record I want to instantiate without having to create objects for the entire table?

Thanks

CraigHartz
 
You could do it several other ways, but I would use a class. :)

If that's so, how can I create a class object based on the record I want to instantiate without having to create objects for the entire table?
That all depends on how you are pulling your data from the tables. Lets say you load it into a DataTable. You find the train you wish to work with and you get the DataRow for that train from that table. You could then pass the DataRow to the class in it's sub New() and set all the information there. So you see the first question is how are you pulling your data from the database? Did you pull your data into a DataTable or is it a BoundSource?

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Guess I'm starting to learn something :)

Yes, data is coming from a data table / data record. I've never trusted Bound objects since VB5 (though I've been reading that in recent versions of VB.net it's gotten a lot better).

So I assume then we are talking about a constructor in the class object to accept the datarow and parse the fields out the properties?



CraigHartz
 
So I assume then we are talking about a constructor in the class object to accept the datarow and parse the fields out the properties?
Yes. As a first choice off what you said that makes the most sense to me.

Bound objects are not bad, but for me I always find I'm fighting them more than they help me so I don't use them.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Okay, just confirming then - The way to do this would be to pass a Datarow object to the class constructor and read out the fields into the properties?

CraigHartz
 
Maybe something like this:
Code:
Public Class TrainClass
    Private Name As String
    Private Track As String
    ....
    Public Sub New(ByVal TrainRow As DataRow)
        Name = TrainRow.Item(0)
        Track = TrainRow.Item(1)
        ....
    End Sub

    ....
End Class

Or something like this
Code:
Public Class TrainClass
    Private TrainDataCollection As New Collection
    ....

    Public Sub New(ByVal TrainRow As DataRow, ByVal ColumnInfo As DataColumnCollection)
        For i As Integer = 0 To TrainRow.ItemArray.Length
            TrainDataCollection.Add(TrainRow.Item(i), ColumnInfo.Item(i).ColumnName)
        Next
    End Sub

    ....
End Class

The second one I'm not 100% sure about as I've not tried it quite like that, but it should come out right.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top