ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Parent property of custom class (https://www.excelbanter.com/excel-programming/421151-parent-property-custom-class.html)

atpgroups

Parent property of custom class
 
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?

Peter T

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?




atpgroups

Parent property of custom class
 
On Dec 11, 2:15*pm, "Peter T" <peter_t@discussions wrote:
One way is to tell the children who their parents are


<Snip Code

OK, I can see that that would work. Is there any risk of a loop? My
first thought with your method was to tell the child who its parent is
in the Class_Initialize event of the parent.
The problem there is that when the Child creates an object (obj in
your example) then that code will run again, because obj has a child.

I think I will save my work before trying this...

atpgroups

Parent property of custom class
 
On Dec 11, 6:12*pm, atpgroups wrote:

The problem there is that when the Child creates an object (obj in
your example) then that code will run again, because obj has a child.


OK, it seems not to be a problem. The code runs through only
once.There is an infinitely deep logical tree in the Watch window, but
that isn't a problem.

Thanks.



All times are GMT +1. The time now is 01:37 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com