The "Right" way to capture events from class arrays?
Posted on 2006-05-14
I want to handle an array of objects, and events originating from each of those objects. Below is what I’ve done; it seems to do what I want, the HandleSignal event will handle an event from either of the classes (e.g., if I call the Go function), and it will have access to all of that object’s members so that it can take the appropriate action based on the source and the state (i.e., member values) of the source.
However, as you can see I have achieved this by including a self-reference in the event declaration (i.e., passing “Me” back as a parameter). This works but seems a little strange/inelegant to me, is this the *Right* way to do this?
Just wondering if I’m missing something…
Public Class myParentClass
Dim tcs(20) As myChildClass
Public Sub New()
tcs(0) = New myChildClass("First")
tcs(1) = New myChildClass("Second")
AddHandler tcs(0).Signal, AddressOf Me.HandleSignal
AddHandler tcs(1).Signal, AddressOf Me.HandleSignal
Public Sub HandleSignal(ByRef tc As myChildClass, ByVal z As Integer)
MsgBox("rx signal from " & tc.Name & ": " & z)
Public Sub Go()
Public Class myChildClass
Public Event Signal(ByRef tc As myChildClass, ByVal z As Integer)
Public Name As String
Public Sub New(ByVal nm As String)
Name = nm
Public Sub SendSignal()
RaiseEvent Signal(Me, Rnd() * 10)