List of Objects and Shared Events
Okay so I've created a list of objects from a class like
For B As Integer = 0 To 5
clients.Add(New Client)
AddHandler clients(B).OnMessage, AddressOf clients_OnRec
Next
Then this is the event declaration
Public Event OnRec As EventHandler
This is my Event
Private Sub clients_OnRec(ByVal sender As Object)
'Does something
End Sub
开发者_Go百科My question is how can i determine which instance of the class in the list raised the event. I need to be able to do something like:
clients(whateveronefiredit).ExecuteMethodInClass
How can i do that?
:) Let me try explaining what is happening
Now in the list you have 5 objects of type Client and all this 5 of them call the event handler clients_OnRec on some kind of event.
When the first client raises this event the sender
in the event handler signature Private Sub clients_OnRec(ByVal sender As Object)
will have the client objects reference which raised the event.
so, in order to call the method ExecuteMethodInClass
on the object which raised the event, you would do the following:
Private Sub clients_OnRec(ByVal sender As Object)
Dim c As Client = CType(sender, Client) 'Cast the sender object as Client object
c.ExecuteMethodInClass() 'This executes the ExecuteMethodInClass on the Client object which raised this event
End Sub
Hope that is clear.
Cheers
精彩评论