View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
Tim Williams[_2_] Tim Williams[_2_] is offline
external usenet poster
 
Posts: 298
Default How do you get a parent collection object to listen to all if it's child objects?

Ronald,

You're right - I hadn't thought it through for the case of a collection or
array of objects.
Not as easy as I'd thought...

One way to do it (without class events as such, though below I'm using a
worksheet field just as an existing object able to raise an "internal"
event...) would be to pass a reference to the "container" object into each
member object as it's added to the collection or array. That way it can use
this reference to create a "callback" to it's "parent" class.

Eg:


' ---- in 'clsSheet' class module
Option Explicit
Private objOwner As Object
Public WithEvents ws As Excel.Worksheet

Private Sub ws_Change(ByVal Target As Range)
If Not objOwner Is Nothing Then
CallByName objOwner, "MemberChanged", 1, Target
End If
End Sub

Sub Init(sht As Excel.Worksheet, Optional owner As Object)
Set ws = sht
If Not owner Is Nothing Then Set objOwner = owner
End Sub
'----- end clsSheet



'------ In another class module (tested in `Sheet1`)
Option Explicit
Private osheets As Collection

Sub Setup()
Dim s As Worksheet, obs As clsSheet
Set osheets = New Collection
For Each s In ThisWorkbook.Worksheets
Set obs = New clsSheet
obs.Init s, Me
osheets.Add obs
Next s
End Sub

Sub MemberChanged(ByRef v As Object)
MsgBox v.Parent.Name & " changed sheet at: " _
& v.Address()
End Sub
'----- end code in other class module

Too much coupling between the member and container code.
Not great, but it will "work".

You might also Google for VBA+"control array" for ideas, depending on what
it is you're looking to set up.

Tim

"Ronald R. Dodge, Jr." wrote in message
...
I understand about creating events in general, but what I want to do is
have either the parent collection object or the parent of that collection
object to listen to all of the child objects of the parent collection
object.

Example:

I know the Event itself goes in the Child Object (Source Class).

I know the raising of the event is in the Child Object (Source Class)

I also know the Source Object must be declared within the Sink Module.

What I don't want to have to do is create a different object variable for
each child object within the sink class module that is suppose to listen
for those events, but rather like to emulate how the "SheetChange" event
is handled.

What I'm getting at, how is it that the "ChangeSheet" within the sink
class module of "ThisWorkbook" module is able to listen to every single
sheet within the workbook rather than to just one sheet?

--
Thanks,

Ronald R. Dodge, Jr.
Production Statistician
Master MOUS 2000
"Tim Williams" wrote in message
...
See: http://www.cpearson.com/excel/Events.aspx

Down towards the bottom of the page.

Tim


"Ronald R. Dodge, Jr." wrote in message
...
I am looking for a way of getting a parent collection object to be able
to hear all of it's child objects via events similar to how the Workbooks
Event of "SheetChange" work in listening to when there's a change to any
of it's sheets. How can I setup a such mechanism?

--
Thanks,

Ronald R. Dodge, Jr.
Production Statistician
Master MOUS 2000