Event Handler Exists?

DennisH

Member
Joined
Jun 22, 2012
Messages
6
Programming Experience
3-5
I add an event handler in my code for a user control depending on the specific operation that is requested:

AddHandler myEvent, Addressof Handle_MyEvent

When the control is disposed of, I want to remove the event handler only if it was added in the first place using:

If "the handler exists" then
RemoveHandler MyEvent, Addressof Handle_MyEvent
end if

However, I cannot find what I need to put in the "the handler exists" to check if it was actually added. I found one idea but it doesn't work:

If MyEventEvent IsNot Nothing then

It says that the MyEventEvent is private and cannot be used in this context. Any ideas on how I can do what I want?
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,517
Location
Norway
Programming Experience
10+
You don't need to check, RemoveHandler does nothing if that delegate is not attached. If it is possible that you have added event handler using AddHandler, but you can't know for sure, just call RemoveHandler to be sure you don't leave a memory leak.
 

DennisH

Member
Joined
Jun 22, 2012
Messages
6
Programming Experience
3-5
You don't need to check, RemoveHandler does nothing if that delegate is not attached. If it is possible that you have added event handler using AddHandler, but you can't know for sure, just call RemoveHandler to be sure you don't leave a memory leak.

Thanks for reply. However, if the handler has not been added and you try to remove it, a Null exception error is generated which, of course you can trap. I just wanted to eliminate the error as it is a control that others use and it can be annoying to fill the Immediate Window with Null Exceptions. Right now, I set a boolean flag when the handler is added then remove it only if the flag was set. Just looking for a more elegant way to check if the handler had been added.
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,517
Location
Norway
Programming Experience
10+
Thanks for reply. However, if the handler has not been added and you try to remove it, a Null exception error is generated
No, that does not happen.
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,397
Location
Sydney, Australia
Programming Experience
10+
As JohnH says, RemoveHandler does nothing if the specified delegate has not already been registered. No exception, null reference or otherwise, is thrown as a result. What you have already been told is the one, only and proper way to do what you want to do.

If you are seeing a NullReferenceException thrown then it is for another reason. My guess would be that the object whose event you're trying to handle doesn't actually exist, but that's just a guess. If you can't figure it out then show us the relevant code and tell us EXACTLY what happens and then we can help you fix it. Regardless, your final code is going to involve the use of RemoveHandler.
 
Top Bottom