Parent property of custom class
One way is to tell the children who their parents are
' normal module
Sub Test3()
Dim c As New Class1
c.Name = "Sally"
End Sub
' code in Class1
Private msName As String
Private mChild As Class2
Public Property Let Name(sName As String)
msName = sName
Breed
mChild.MyParent
End Property
Public Property Get Name() As String
Name = msName
End Property
Sub Breed()
Set mChild = New Class2
Set mChild.Parent = Me
End Sub
' code in Class2
Private mother As Object ' or Class1 if known
Public Property Set Parent(obj As Object)
Set mother = obj
End Property
Public Sub MyParent()
MsgBox mother.Name
End Sub
Regards,
Peter T
"atpgroups" wrote in message
...
I have a custom class and one of its properties is another custom
class
Specifically I have a class which interfaces to an external
application to grab data, and a generic sub-class which works out
statistics.
pseudo-code...
set V = new clsCalItem
V.Stats.reset
while not finshed
V.stats.add
wend
msgbox V.stats.count, v.stats.mean
and clsCalItem contains the line
dim Stats as clsStats
My problem is that to get the current numerical value of V I would use
V.Value, but I can't see how to access the .Value method of V with
code inside clsStats.
What I want is basically to be able to use, inside the clsStats code a
line something like:
internalSum = internalSum + Me.Parent.Value
There is a very simple way round this, of course, I can change the
V.Stats.Add line to be V.Stats.Add(V.Value) but I would prefer the
elegance of the first version, and I am rather intrigued about whether
it is possible now. I can see myself wanting to write a .Parent
property in the future in situations where there isn't this
workaround. Surely classes often need to know who or what they are a
subclass of?
|