View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Ronald R. Dodge, Jr.[_2_] Ronald R. Dodge, Jr.[_2_] is offline
external usenet poster
 
Posts: 134
Default How do you get a parent collection object to listen to all if it's child objects?

Here's the only thing that I can think of.

Child Object must have a Parent Collection Object Property

At the time when the Child Object is created and added to the Parent
Collection Object, a reference to the Parent Collection Object is set in the
Parent Object Property of the Child.

Parent Collection Object must then have both the Event setup and a public
method that retrieves a pointer to the Child Object (via the ByVal method,
not the ByRef method), and then raise the Event within the Parent Collection
Object passing on the reference of the Child Object.

The Sink Module with the declared variable object using the WithEvents
keyword within the declaration statement for the Parent Collection Object is
then able to listen to the Event and know which child object triggered it

The child object may call on it's parent public method to raise the event
rather it be directly from within a property or a method of the child
object.

Example:

Public Property Let prp_rw_Parent(ByVal l_objWorkOrders As clsWorkOrders)
m_objWorkOrders = l_objWorkOrders
End Property
Public Property Get prp_rw_Parent() As clsWorkOrders
prp_rw_Parent = m_objWorkOrders
End Property


Public Property Let prp_rw_Status(ByVal l_lngStatus As g_WorkOrderStatus)
m_dteEndTime = Now()
'This is needed to raise the parent collection event so as production
times
' can be recorded against the work order. For JDE requires data to be
' recorded separately from the work order itself. Production times for
' each line is also needed to be taken into account.
m_objWorkOrders.RaiseOrderStatusUpdate Me
'After production times has been recorded, the status of the job may be
updated.
m_lngStatus = l_lngStatus
m_dteStartTime = m_dteEndTime
End Property.

The parent collection object would then raise the event with this child
object variable passed on.

--
Thanks,

Ronald R. Dodge, Jr.
Production Statistician
Master MOUS 2000
"Tim Williams" wrote in message
...
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