Child Object referencing it's Parent?

Hi Guys,

Forgive what may be a trivial question but I’m new to OOP, especially in Xojo.

Ok, here goes …

I have a Class called ‘Client’ and it has an array of objects called ‘Assessments()’ … populated on one of the Client class constructors

I would like to be able to refer back to the ‘parent’ Client classes from one of the Assessment objects.

In my mind the code would look something like:


… where ‘parent’ refers to the Client object that contains the Assessment object that I’m in?
I don’t always have the appropriate Client object available when dealing with Assessments so I can’t just do: Client.Assessments(i).property

Hope that makes sense …

Thanks in advance.

Pass the parent object to the child object’s constructor and have the child object store the reference as a property. Because the parent class already holds a reference to the child class, you should use a WeakRef to store the parent reference in order to avoid a circular reference.

Class Assessment
Private Property ParentRef As WeakRef

Sub Constructor(Parent As Client)
ParentRef = New WeakRef(Parent)
End Sub

Function Parent() As Client
Dim ref As Client = Client(ParentRef.Value)
Return ref
End Function
End Class[/code]


Dim a As New Assessment(theparent) a.Parent.Assessments(i).Property

Thank Andrew, I’ll check it out and see if I can make it work.

Much appreciated. :wink:

Hey Andrew … that’s &##$$@#@ BRILLIANT mate!!!

Many, many thanks. :wink: