Classes Newbie

C

Chip Mayan

I need a little help, of coarse!

I have created a class with get and set procedures (below). I want to
"fill" the class with information from a table and be able to access
that information like I would an array or a dataset. How do I set the
information in the class, and how do I get it back from the class. I
know how to open the table and walk through it, I just need to know
how to work with the class to store and retrieve information. Any
help would be great!

Thanks,

Chip

<Serializable()> Public Class BaseClass
Private _Description
Private _Code
Private _Matl
Private _ValveSize
Private _EndConn
Private _Act
Private _Pack
Private _TrimForm
Private _TrimMatl

Public Property Description() As String
Get
Description = _Description
End Get
Set(ByVal Value As String)
_Description = Value
End Set
End Property
Public Property Code() As String
Get
Code = _Code
End Get
Set(ByVal Value As String)
_Code = Value
End Set
End Property
Public Property Matl() As String
Get
Matl = _Matl
End Get
Set(ByVal Value As String)
_Matl = Value
End Set
End Property
Public Property ValveSize() As String
Get
ValveSize = _ValveSize
End Get
Set(ByVal Value As String)
_ValveSize = Value
End Set
End Property
Public Property EndConn() As String
Get
EndConn = _EndConn
End Get
Set(ByVal Value As String)
_EndConn = Value
End Set
End Property
Public Property Act() As String
Get
Act = _Act
End Get
Set(ByVal Value As String)
_Act = Value
End Set
End Property
Public Property Pack() As String
Get
Pack = _Pack
End Get
Set(ByVal Value As String)
_Pack = Value
End Set
End Property
Public Property TrimForm() As String
Get
TrimForm = _TrimForm
End Get
Set(ByVal Value As String)
_TrimForm = Value
End Set
End Property
Public Property TrimMatl() As String
Get
TrimMatl = _TrimMatl
End Get
Set(ByVal Value As String)
_TrimMatl = Value
End Set
End Property
Public Overrides Function ToString() As String
Return _Code
End Function
End Class
 
T

Tom Leylan

Chip Mayan said:
I have created a class with get and set procedures (below). I want to
"fill" the class with information from a table and be able to access
that information like I would an array or a dataset.

Chip: It wasn't "too long" but you can leave some properties out and still
get the point across :) Note also that you haven't declared a variable
type in your properties for instance: Private _Description as String

Typically you would add a "load" method which which takes a primary key as a
parameter. The Load() method would gather the data from whatever source (s)
it required (probably a database) and assign the properties. Then you can
access like so...

Dim bc as BaseClass = New BaseClass()

bc.Load( 12345 )

? bc.Description
? bc.Code

etc.
 
R

Rick Mogstad

Tom Leylan said:
Chip: It wasn't "too long" but you can leave some properties out and still
get the point across :) Note also that you haven't declared a variable
type in your properties for instance: Private _Description as String

Typically you would add a "load" method which which takes a primary key as a
parameter. The Load() method would gather the data from whatever source (s)
it required (probably a database) and assign the properties. Then you can
access like so...

Dim bc as BaseClass = New BaseClass()

Could you not just override this so you could put

Dim bc as BaseClass = New BaseClass( 12345 )

I am only asking because I dont know.
 
C

Chip Mayan

Thanks for your quick reply!

So the load method would be in the class? If so then I would set it up
to run when the form was loaded. I still don't quite get how to
retrieve the correct information from the class.

Thanks again,

Chip
 
V

Versteijn

it required (probably a database) and assign the properties. Then you can
Could you not just override this so you could put

Dim bc as BaseClass = New BaseClass( 12345 )

I am only asking because I dont know.

Of course. That's a matter of implementation. Question is though
whether or not a random programmer gets the right meaning from this
call. The constructor (New) is often not associated with loading data
from a database. A call to a Load-like method makes explicitly clear
that something is loaded from the database.

Good luck.

Freek Versteijn
 
T

Tom Leylan

Chip Mayan said:
So the load method would be in the class? If so then I would set it up
to run when the form was loaded. I still don't quite get how to
retrieve the correct information from the class.

Yes it would be implemented in the class (typically) and there would
probably be a Save() method also.

As Rick mentioned it is also possible to "overload" meaning create a second
contructor so that it would "load the data" if you pass it an ID number but
wouldn't if you didn't. Rick used the word override however which isn't the
same thing. (Got that Rick?) Override would replace a constructor that
exists in a base class with a new constructor.

As to when it "runs" that is entirely up to you. Generally speaking it
can't run when the form loads because you aren't likely to know the ID at
that time. You might in which case you could load the object of course.

You should read an article or two about classes and objects (including
instantiation) so you get a basic understanding what it is doing.

Dim theCustomer as CustomerClass

Creates a reference to an object of type CustomerClass but it isn't an
object yet. It reserves enough space to hold on to a reference to an object
(not enough space to hold the data within it however.)

You can set the reference to Nothing at this point but you can't reference
a property, they aren't there.

theCustomer = New CustomerClass()

Now the customer object exists in memory and you can type ? theCustomer.Name
and the name (probably blank) should print. You should be able to set the
name also with theCustomer.Name = "Chip Mayan"

If you wanted to fill the customer with data from a database you can have a
method named anything but "Load" works and you would type.

theCustomer.Load()

but of course it wouldn't know what customer so you usually include an ID

theCustomer.Load( 12345 )

Now the customer should be filled with data.

? theCustomer.Name
? theCustomer.Address
? theCustomer.PhoneNumber

I don't know where your level of knowledge cuts off so I'd suggest that if
you don't know this stuff that you concentrate on this stuff first. You can
even create a dummy Load() method that simply fills in the properties by
assigning them. You don't have to actually access a database to test it
out.

Generally speaking as to your question about "when" it all depends upon what
you want to do. Create the object when you need it, load it when you want
to load it, dispose of it when you no longer need it. No earlier and no
later than that.

In any case... best of luck and read, read, read!

Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top