AddHandler / RemoveHandler

  • Thread starter Thread starter Timo
  • Start date Start date
T

Timo

I assume its a good practice to remove handlers attached to an object's
events before destroying the object?

Is there a way to examine an object to get a list of eventhandlers that have
been attached, via AddHandler, to the object's events, so a method can
iterate through that list and invoke RemoveHandler for each item in it?

Thanks!
Timo
 
Well, there's a question mark at the end of that statement ;-)

My assumption was that these handlers are objects which might become
"orphaned" somehow,and cause a problem, if the objects whose events they
were added to have been destroyed.

Do I understand from your own socratic question that I can dynamically add a
bunch of controls to my form, add handlers to specific events for these
controls, and then remove those controls from the form, and then add new
ones, possibly with the same names and with their own set of comparable
handlers, all without undesirable side-effects?

To be specific: I'm working with a grid control that lets you add rows
dynamically, changing cell editors as required by the datatype in question.
I'm using the same grid as a front-end to various database tables. Depending
upon a user's menu selection (Employees, Vendors, Customers, etc) I delete
all the rows in the grid and then recreate the grid with new rows, editors,
and handlers appropriate for the table in question. Some of the tables have
100 columns, so there could be quite a few of these "left-over" handlers
hanging around -- if, indeed, they do hang around?

Thanks
Timo

Alvin Bruney said:
why do you think it is a good practice?

--
Regards,
Alvin Bruney [ASP.NET MVP]
Got tidbits? Get it here...
http://tinyurl.com/3he3b
Timo said:
I assume its a good practice to remove handlers attached to an object's
events before destroying the object?

Is there a way to examine an object to get a list of eventhandlers that have
been attached, via AddHandler, to the object's events, so a method can
iterate through that list and invoke RemoveHandler for each item in it?

Thanks!
Timo
 
Timo,
I normally reserve RemoveHandler for the object that called AddHandler.

For example if I call AddHandler in the Add method of a custom collection, I
will call RemoveHandler in the Remove method of the same custom collection.

I would question a design that decides an object will remove all the
handlers, even handlers it did not add...

When you add an Event to a VB.NET class or structure, a private hidden field
is added to the class or structure that has the same name as the Event
followed by Event of type Delegate. You can use this field to get the list
of event handlers. My concern is this is not documented, so relying on it
may break your code when you move to future versions of VB.NET.

Public Class Timo

Public Event Changed As EventHandler

Public Function AnyHandlers() As Boolean
Return ChangedEvent.GetInvocationList().Length <> 0
End Function

Check out the Delegate.GetInvocationList function.

Hope this helps
Jay
 
Back
Top