Remove all Handlers associated with an Event

vendiddy

Well-known member
Joined
May 14, 2006
Messages
64
Programming Experience
1-3
Say I have
VB.NET:
public event myEvent()
in a class called myClass.
and somewhere in my program, I write
VB.NET:
AddHandler myClass.myEvent, AddressOf method1
AddHandler myClass.myEvent, AddressOf method2
AddHandler myClass.myEvent, AddressOf method3

Is there a way for myClass to remove all of the handlers associated with myClass.myEvent? (Or must I manually keep track of all of the handlers?)

Thanks.
 
"myEvent" have an internally declared delegate "myEventEvent" that you can set to Nothing, but it would be bad design and foul play against the objects that have subscribed now without warning or notification have lost this event, wouldn't it?
 
reasoning & another question

My reasoning for wanting to remove all handlers is that I'm creating a graph drawing program (nodes & edges). When I change modes (say from dropping down nodes to creating edges), I want to do a little bit of cleanup and then remove all the paint event handlers, mouse move handlers, etc. I'll think this through to decide whether I really need to do it this way.

Is there a better, i.e. good practice, way that I could do this?

Also, another question: If you remove all references to an object but you don't remove any handlers to the object's event, will the object be garbage collected? Or does an event handler count as a reference, requiring all handlers to be removed for it to be garbage collected? In other words, should I remove all event handlers to an object's event before 'disposing' of the object?

Thanks!
 
Paint and MouseMove are events of Control and you can't get inside this class to get hold of the private event delegate.

If you dispose an object the event "references" to this object is removed (because it is the object itself that keeps track of these).
 
Back
Top