Don't Subscribe to Your Own Events?
Help me out here, guys.
A long while back I remember reading an article about designing controls in .NET, and one of the guidelines the author put forward was, "don't subscribe to your own events."
Basically he was saying that a control shouldn't make use of its own public events. For example, if your have a Click event, and you need to take some custom action when the user clicks on your control, you shouldn't handle your own Click event. Instead you should put your custom code in the OnClick protected virtual method.
Does this ring a bell to anyone out there? Where would I have read this?
I've started taking this on board with Form events. I try not to handle Form.Load any more: instead I override the protected OnLoad method. That way I'm not using the event subscription mechanism to call my code, and it also makes it really easy to remove the code (since there's no extra bits in Form1.Designer.cs to worry about).

Comments
# crucible
18/04/2006 10:39 PM
okay, being a beginner and having no clue - this is what I'm thinking...
a) by using the OnClick you still allow the click method of your control for the coder using it to use at a later stage without fear of overriding your code...
b) you have your control set up the way you want, without fear of overriding your functions at a later stage and breaking something.
ie: if you build a control and use the Load event, and I want to do something on the controls load - I'm not going to break your code, and you dont have to worry about me breaking your control code by using the Load event.
I don't know if that remotely makes sense or if I'm on the right track here.
# mabster
19/04/2006 8:32 AM
Actually, handling your own event doesn't preclude others from handling it - events in .NET are multicast and can have many subscribers.
I'm thinking that it's more to do with performance than anything, but I particularly like the idea that I can delete the method override in my Form1.cs file and not have to worry about going to Form1.Designer.cs and deleting the line that adds a handler to an event.
# Matth
11/07/2006 4:02 AM
I had to ask the guy who referred that article to me . . . is the the one blog post that Google has missed??
http://weblogs.asp.net/savanness/archive/2003/03/10/3646.aspx