|
-
Jul 6th, 2015, 06:40 PM
#1
Thread Starter
Addicted Member
[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
-
Jul 6th, 2015, 07:00 PM
#2
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
-
Jul 6th, 2015, 07:10 PM
#3
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.
Burn the land and boil the sea
You can't take the sky from me
~T
-
Jul 6th, 2015, 08:02 PM
#4
Thread Starter
Addicted Member
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
-
Jul 6th, 2015, 08:09 PM
#5
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
-
Jul 6th, 2015, 08:22 PM
#6
Thread Starter
Addicted Member
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.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|