Raising events from objects in a collection 
Author Message
 Raising events from objects in a collection

This looks like a 'Many to One' problem.

You want all the Doors to raise an event - and to identify which Door
it is.

If you have a Class then you can only set one instance of that Class
into One  'Dim WithEvents MyDoor'  Template.

The way Controls do this is by being Control Arrays - so you get an
Index passed in - at that point one is off.
You could make each Door a UserControl - and have a control array of
user controls.

Alternatively you could have a Class that 'Fronts' the Doors and pass
the actual object over in the event.

I have  knocked up an example of this  - One Form, Two Classes :-

============== FORM1.FRM =================

Option Explicit

Dim WithEvents DoorMgr As clsDoorMgr

Dim Door1 As New clsDoor

' --- FORM1  -  Add Three Command Buttons

Dim Door2 As New clsDoor

Private Sub Command1_Click()
    Static AllDoors As New clsDoorMgr
    Set DoorMgr = AllDoors
End Sub

Private Sub Command2_Click()
    DoorMgr.RegisterDoor Door1
    Door1.Name = "Door One"
    DoorMgr.RegisterDoor Door2
    Door2.Name = "Door Two"
End Sub

Private Sub Command3_Click()
    Door1.DoorOpen
    Door2.DoorOpen
End Sub

Private Sub DoorMgr_DoorIsOpened(D As clsDoor)
    MsgBox "Door is Open " + D.Name
End Sub

Private Sub Form_Initialize()
    Command1.Caption = "Initialize DoorMgr"
    Command2.Caption = "Register Doors"
    Command3.Caption = "Open Doors"
End Sub

========================================

' --- clsDoorMgr.cls

Option Explicit

Public Event DoorIsOpened(D As clsDoor)

Sub RegisterDoor(D As clsDoor)
   Set D.Parent = Me
End Sub

Public Sub DoorIsOpened(D As clsDoor)
    RaiseEvent DoorIsOpened(D)
End Sub

========================================

' --- clsDoor.cls

Option Explicit

Public Event DoorIsOpened(D As clsDoor, V$)

Private mParent As clsDoorMgr, _
        mName As String

Public Property Set Parent(Value As clsDoorMgr)
   Set mParent = Value
End Property

Public Property Let Name(Value$)
   mName = Value
End Property

Public Property Get Name$()
   Name = mName
End Property

Public Sub DoorOpen()
   Call mParent.DoorIsOpened(Me)
End Sub

=======================================

Quite an interesting exercize -

=======================================

On Tue, 20 Aug 2002 00:36:22 -0500, "Mike Schilling"

Quote:

>Does anyone know of some type of routine to respond to events of objects in
>a collection.
>For example:

>-objDoor is an object which has an "Opened" event
>-colDoors is a collection of objDoor objects

>To repond to the event with just an object, you would code:

>Sub objDoor_Opened()
>'code when door has opened
>'yadda yadda yadda
>End Sub

>But what if there were several - or several hundred - objDoor "items"
>(objects) in an objDoors collection, and one of them doors opened. How would
>you respond to that?



Wed, 18 Jun 1902 08:00:00 GMT  
 Raising events from objects in a collection
Why not simply pass a reference to to the door as a parameter in the
event routine?
Call SomeOuterClass.RaiseOpenedDoorEvent(Me)
--
Roger Abbott, RHA (Minisystems) Ltd - http://www.rhaminisys.com
DDE Client and Server ActiveX controls for Visual Basic
DDE FAQ and DDE utility tools, browser URL monitor,
Program launcher/setup menu, other freeware and shareware


Quote:

> This looks like a 'Many to One' problem.

> You want all the Doors to raise an event - and to identify which Door
> it is.

> If you have a Class then you can only set one instance of that Class
> into One  'Dim WithEvents MyDoor'  Template.

> The way Controls do this is by being Control Arrays - so you get an
> Index passed in - at that point one is off.
> You could make each Door a UserControl - and have a control array of
> user controls.

> Alternatively you could have a Class that 'Fronts' the Doors and pass
> the actual object over in the event.

> I have  knocked up an example of this  - One Form, Two Classes :-

> ============== FORM1.FRM =================

> Option Explicit

> Dim WithEvents DoorMgr As clsDoorMgr

> Dim Door1 As New clsDoor

> ' --- FORM1  -  Add Three Command Buttons

> Dim Door2 As New clsDoor

> Private Sub Command1_Click()
>     Static AllDoors As New clsDoorMgr
>     Set DoorMgr = AllDoors
> End Sub

> Private Sub Command2_Click()
>     DoorMgr.RegisterDoor Door1
>     Door1.Name = "Door One"
>     DoorMgr.RegisterDoor Door2
>     Door2.Name = "Door Two"
> End Sub

> Private Sub Command3_Click()
>     Door1.DoorOpen
>     Door2.DoorOpen
> End Sub

> Private Sub DoorMgr_DoorIsOpened(D As clsDoor)
>     MsgBox "Door is Open " + D.Name
> End Sub

> Private Sub Form_Initialize()
>     Command1.Caption = "Initialize DoorMgr"
>     Command2.Caption = "Register Doors"
>     Command3.Caption = "Open Doors"
> End Sub

> ========================================

> ' --- clsDoorMgr.cls

> Option Explicit

> Public Event DoorIsOpened(D As clsDoor)

> Sub RegisterDoor(D As clsDoor)
>    Set D.Parent = Me
> End Sub

> Public Sub DoorIsOpened(D As clsDoor)
>     RaiseEvent DoorIsOpened(D)
> End Sub

> ========================================

> ' --- clsDoor.cls

> Option Explicit

> Public Event DoorIsOpened(D As clsDoor, V$)

> Private mParent As clsDoorMgr, _
>         mName As String

> Public Property Set Parent(Value As clsDoorMgr)
>    Set mParent = Value
> End Property

> Public Property Let Name(Value$)
>    mName = Value
> End Property

> Public Property Get Name$()
>    Name = mName
> End Property

> Public Sub DoorOpen()
>    Call mParent.DoorIsOpened(Me)
> End Sub

> =======================================

> Quite an interesting exercize -

> =======================================

> On Tue, 20 Aug 2002 00:36:22 -0500, "Mike Schilling"

> >Does anyone know of some type of routine to respond to events of
objects in
> >a collection.
> >For example:

> >-objDoor is an object which has an "Opened" event
> >-colDoors is a collection of objDoor objects

> >To repond to the event with just an object, you would code:

> >Sub objDoor_Opened()
> >'code when door has opened
> >'yadda yadda yadda
> >End Sub

> >But what if there were several - or several hundred - objDoor "items"
> >(objects) in an objDoors collection, and one of them doors opened.
How would
> >you respond to that?

Sent via Deja.com http://www.deja.com/
Before you buy.


Wed, 18 Jun 1902 08:00:00 GMT  
 Raising events from objects in a collection
Test


Wed, 18 Jun 1902 08:00:00 GMT  
 Raising events from objects in a collection
Why is your message/clock two (2!) Years ahead?  Or more importantly for me,
why did my Usenet server let the message propagate down to me?  I'm not
allowed to post anything with the wrong date (it must use the NNTP server's
date for messages).

--
Howard Henry Schlunder
 Winamp is currently playing:
 mariah carey--dream lover


Quote:
> Does anyone know of some type of routine to respond to events of objects
in
> a collection.
> For example:

> -objDoor is an object which has an "Opened" event
> -colDoors is a collection of objDoor objects

> To repond to the event with just an object, you would code:

> Sub objDoor_Opened()
> 'code when door has opened
> 'yadda yadda yadda
> End Sub

> But what if there were several - or several hundred - objDoor "items"
> (objects) in an objDoors collection, and one of them doors opened. How
would
> you respond to that?



Wed, 18 Jun 1902 08:00:00 GMT  
 Raising events from objects in a collection
Hi Howard,
This keeps the message listed at the top of a date sorted list.



Quote:
> Why is your message/clock two (2!) Years ahead?  Or more importantly for
me,
> why did my Usenet server let the message propagate down to me?  I'm not
> allowed to post anything with the wrong date (it must use the NNTP
server's
> date for messages).

> --
> Howard Henry Schlunder
>  Winamp is currently playing:
>  mariah carey--dream lover



> > Does anyone know of some type of routine to respond to events of objects
> in
> > a collection.
> > For example:

> > -objDoor is an object which has an "Opened" event
> > -colDoors is a collection of objDoor objects

> > To repond to the event with just an object, you would code:

> > Sub objDoor_Opened()
> > 'code when door has opened
> > 'yadda yadda yadda
> > End Sub

> > But what if there were several - or several hundred - objDoor "items"
> > (objects) in an objDoors collection, and one of them doors opened. How
> would
> > you respond to that?



Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Raising events from objects in a collection

2. Raising events from objects in a collection

3. Raising events from objects in a collection???

4. Raising events from objects in a collection???

5. Raising events from objects in a collection???

6. Raising events from objects in a collection???

7. Raising events from objects in a collection???

8. Raising events from objects in a collection???

9. Objects in Collection and Raise Events

10. Raising events from collection member objects?!?!

11. Raising events from a class within a collection class

12. Raise event from Collection member??

 

 
Powered by phpBB® Forum Software