riceman0
asked on
The "Right" way to capture events from class arrays?
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
End Sub
Public Sub HandleSignal(ByRef tc As myChildClass, ByVal z As Integer)
MsgBox("rx signal from " & tc.Name & ": " & z)
End Sub
Public Sub Go()
Call tcs(1).SendSignal()
End Sub
End Class
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
End Sub
Public Sub SendSignal()
RaiseEvent Signal(Me, Rnd() * 10)
End Sub
End Class
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
End Sub
Public Sub HandleSignal(ByRef tc As myChildClass, ByVal z As Integer)
MsgBox("rx signal from " & tc.Name & ": " & z)
End Sub
Public Sub Go()
Call tcs(1).SendSignal()
End Sub
End Class
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
End Sub
Public Sub SendSignal()
RaiseEvent Signal(Me, Rnd() * 10)
End Sub
End Class
ASKER
So I lucked on to the "right" way... how unusual for me.
BTW, so that the purpose of the "sender" parameter? How do you get anything useful (i.e., button-specific, like a caption) out of a System.Object?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks.
.Net does the same thing. The events in .Net have the "sender" parameter:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
End Sub
So when Button1 raises its Click() event, it sends "Me", which becomes the "sender" parameter...just as you are doing.