class parent property - passing variables on instantiation?

S

sparkane

I have a question about defining a Parent property in a VBA class.

In Getz et al.'s Access 2000 handbook, he provides an example of
defining an object's parent. He says, let's say you want to see the
Invoice object from the Customer object it contains. (Not sure why an
Invoice would contain a Customer, seems like it should be the other way
around, but whatever.) He says the following code would appear in the
_Invoice_ class:

Private Sub Class_Initialize()
Set Customer.Parent = Me
End Sub

Now this seems all fine and dandy as long as the _Customer_ object
already has been defined. That is, this approach works if the parent
object is defined after the child object. But what if someone (very
similar to myself) wants to instantiate the child object after the
parent object? So e.g. we have an Invoice object floating around
somewhere, and now we want to use its Customer child object, and we want
that Customer's Parent property to refer back to this Invoice.

Being something of a Perl-head, I keep wanting to pass an object
variable to the Customer's constructor, a la:

_Customer constructor_
Private moParent As Invoice

Private Sub Class_Initialize(o As Invoice)
Set moParent = o
End Sub

But this hasn't worked for me. The only solution I've come up with is
to use two lines of code in the parent object's routine instantiate the
child object, a la:

_Invoice object routine instantiating Customer object_
Private moCustomer As Customer

Public Property Get Customer() As Customer
If moCustomer Is Nothing Then
Set moCustomer = New Customer
moCustomer.Parent = Me

End If

Set Customer = moCustomer
End Property

Does anyone have any advice about this?
 

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