AddHandler & RemoveHandler:
AddHandler: Allows at run time to decide which runtime should serve a given event. AddHandler routine should serve a given event. AddHandler routine takes two arguments (i) the event we want to handle and (ii) the address of the routine that handles the routine.
Example: Dim Log1 As Logger //Logger is a class
AddHandler Log1.LogAction, AddressOf LogActionEvent
The counterpart of AddHandler is RemoveHandler. Both AddHandler & RemoveHandler are key words of VB.
RemoveHandler: Log1.LogAction, AddressOf LogActionEvent.
If an event has been added using AddHandler than make sure that RemoveHandler is also used, else there might be unforeseeable consequences. For Example, if AddressHndler is used of FormA to trap events raised by FormB, the AddHandler would create a hidden reference inside FormB that points to FormA. If RemoveHandler is not explicitly called, then that reference remains alive. WithEvent keyword ties an EventHandler to a specific variable, whereas the AddHandler command ties an event handler to a specific object.
Add a comment