Arguments for Class Object Methods ...

G

Guest

Hi everyone,

I wrote a Class Module for the Customer object, "clsCustomer". It has the
following methods, each has an argument, "strCustomerID".

Public Function Age(strCustomerID As String) As Integer

Public Function CreditScore(strCustomerID As String) As Integer

Public Function FullAddress(strCustomerID As String, _
Optional strTargetAddress As AddressType = Correspondence) As String

Public Function FullName(strCustomerID As String, _
Optional AddMiddleName As Boolean = True) As String

etc etc .....

Now, isn't that ugly to have to feed each method of the class with the same
argument ?

I can't figure out a way of doing it in one clean stroke, I mean, to provide
the strCustomerID to the class itself at the time of instantiating, and feel
free to use any of its methods (which obviously take the same argument,
amongst other optionals).

Any help is greatly appreciated.
 
G

Guest

May be, weekends are a wrong time to post a thread ! :)

I'm new to Class Modules and I've simply put the functions in my standard
modules into a class module and hoping to pull up a miracle. I guess, those
functions need to be modified to act as methods of the class objects, which
I'm unable to figure out how.

Can someone point out what should be the structure of a clsCustomer object
that has methods acting through the strCustomerID property ?
 
G

Guest

Hi everyone,

Here is my latest update.

I have realised the methods I'm using are actually properties of the
clsCustomer Object. With some reading on Property Procedures,I've revised my
class as follows. Please someone tell me I'm doing it right.

Private strCustomerID As String
Private strFullName As String
Private intAge As Integer
Private strPhoneList As String
Private intCreditScore As Integer

Public Property Get CustomerID() As String
CustomerID = strCustomerID
End Property

Public Property Let CustomerID(strNewID As String)
strCustomerID = strNewID
End Property

Public Property Get FullName(Optional AddMiddleName As Boolean = False) As
String

Public Property Get Age() As Integer

Public Property Get CreditScore() As Integer

Public Property Get PhoneList(Optional strPhoneType As PhoneType =
HomePhone) As String

etc etc ........


(I've avoided the Property Let procedures as these properties are "Read
Only". Am I getting it right ?)
 
J

John Nurick

Hi Sreedhar,

This looks reasonable. Obviously you need a private procedure that takes
strCustomerID as an argument and retrieves the various property values
from the data store; you'd call this procedure from the constructor and
from the Property Let CustomerID() procedure, e.g. (air code):

Public Property Let CustomerID(strNewID As String)
On Error GoTo ErrHandler:
RetrieveCustomerPropertiesFromDataStore(strNewID)
strCustomerID = strNewID
Exit Property
ErrHandler:
strCustomerID = "#ERROR#"
...
End Property

Are you sure you want the other properties to be read-only? (i.e. are
you sure you don't want to be able to update a credit score or phone
number?)

I presume the data store contains DateOfBirth rather than Age (because
if you store Age it has to be updated every birthday). Should you expose
DateOfBirth as a property as well as the calculated value Age?
 
G

Guest

Hi John,
Are you sure you want the other properties to be read-only? (i.e. are
you sure you don't want to be able to update a credit score or phone
number?)

The credit score is a calculated value from about 20 - 25 fields from
tblCustomer. The PhoneList is a string that lists out all the available
phones for a given customer. I think these are useful in my code for
producing the output. For data updation, I'm relying on bound forms. Don't
know the possibilities of using class objects for data updation.
I presume the data store contains DateOfBirth rather than Age (because
if you store Age it has to be updated every birthday). Should you expose
DateOfBirth as a property as well as the calculated value Age?

Yes. The underlying field is DateOfBirth. Age is a calculated value.

I've a lot of dilemmas at this stage. I would like to recieve your advice on
exposing fields in the underlying table as properties of the class object.
Isn't that a lot of code ? I thought it was worth only for calculated values.
If not, can you give me pointers on ways to use such things ?

Thanks for the reply.
 
J

John Nurick

Comments inline:

Hi John,


The credit score is a calculated value from about 20 - 25 fields from
tblCustomer. The PhoneList is a string that lists out all the available
phones for a given customer. I think these are useful in my code for
producing the output. For data updation, I'm relying on bound forms. Don't
know the possibilities of using class objects for data updation.

If you're not sharing the data with other users or processes, all you
need to do is use the class's destructor to write any changed values
back to the data store (e.g. with update queries or recordset
operations)

But if others may be updating the data store, things get more
complicated. I guess you'd need either to lock the underlying data (e.g.
records in one or more tables) during the life of the object, or else
have every Property Get/Let read/write the relevant data from/to the
store.
Yes. The underlying field is DateOfBirth. Age is a calculated value.

I've a lot of dilemmas at this stage. I would like to recieve your advice on
exposing fields in the underlying table as properties of the class object.
Isn't that a lot of code ? I thought it was worth only for calculated values.

AIUI you can't expose them directly; either you include code in the
constructor to get the values from the table and store them in the
"private" variables associated with the various properties ready to be
accessed in Property Get procedures; or else use procedures like this to
get each value from the table as required.

Property Get XXX() As Variant
XXX = DLookup("XXX", "My Table", "CustomerID = '" & strCustomerID)
End Property

But I'm not an expert in this area. Most of the time it seems enormously
simpler to work with bound forms.
 

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