[RESOLVED] Is therea way to handle events in a sorted list?
Hi,
Suppose I have this class which can raise an event:
Code:
Public Class eRaiser
Event sendInfo(ByVal info As String)
Public Sub InfoEventRaiser(ByVal info As String)
RaiseEvent sendInfo(info)
End Sub
End Class
If I have one of these, I can make an event handler:
Code:
Public WithEvents er As New eRaiser
Sub erHandler(ByVal info As String) Handles er.sendInfo
End Sub
Now, let's suppose I need to have a sorted list of these:
Code:
Public WithEvents ers As New SortedList(Of Integer, eRaiser)
Is there a way to reference the event individually or collectively?
Code:
Sub ersHandler(ByVal info As String) Handles ers???
Thanks
Re: Is therea way to handle events in a sorted list?
You would need to use AddHandler to wire up the event to the eventhandler at run time when you create the eRaiser object and add it to your list.
Code:
Sub ersHandler(ByVal info As String) 'No handles
... do your thing ...
Code:
Sub AddNeweRaiser()
dim er as new eRaiser
AddHandler er.sendInfo, AddressOf ersHAndler
ers.Add(ers.count +1, er)
end sub
Something along those lines... you create the individual object, wire it up to your handler, then add it to your list.
-tg
Re: Is therea way to handle events in a sorted list?
I would make the SortedList part of the class and pass into an event so it could be consumed when the event is called.
Re: Is therea way to handle events in a sorted list?
Wow. Two very fast replies, thanks.
Could you also suggest a way that the handler would know which er (key) raised the event?
Thanks
Re: Is therea way to handle events in a sorted list?
Well if you follow the standard convention of sender, eventargs... then it would be easy...
Code:
Public Class eRaiser
public class eRaiserEventArg
inherits SystemEventArgs 'I think that's right.
public property Info as string
end class
Event sendInfo(ByVal sender as object, ByVal e As eRaiserEventArg) '
Public Sub InfoEventRaiser(ByVal info As String)
Dim evtArg as new eRaiserEventArg with {.Info = info}
RaiseEvent sendInfo(me, evtArg)
End Sub
Then your handler stub looks like this:
Code:
Sub ersHandler(byval sender as object, byval e as eRaiserEventArg)
'Now Sender is the object that riased the event
Should all look somewhat familiar as it's the same pattern nearly every control and object follows.
-tg
Re: Is therea way to handle events in a sorted list?
Thanks for the help - and for setting me straight on the wisdom of using standard arguments sender and e.