Re: Overriding "OnXXX" versus adding event handler

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Peter

for the sake of discussion

I have impression that main point is dynamic control. As you might know
event handlers could be changed on the fly, though maybe not always very
easy. Overrides are declarative, so you can change them only if you
regenerate assembly. So, objects with events behave more naturally and are
more flexible, generally speaking.

Also, on the more general side, "always" for new functionality is not that
good if you consider how quickly everything changes. New functions become
obsolete very quickly. Ideally, any component should be easily changed to
new version. Events are better suited for this versioning as they are by
nature dynamic. Overrides are too "hard-coded" for this.

Also I saw one point about "easier" to maintain. I do not agree overrides
are easier to maintain. No specific arguments yet in addition to above, but
somehow doesn't seem valid to me.

If event structure is designed properly, I would rather stay with events -
always. Main argument - to maximize re-use and maintainability I prefer not
to touch base functionality and not to deal with component (black boxes)
internals - of course, if only I don't develop component itself. If there is
a need to modify base class functionality, first what comes to my mind is
"design deficiency". Either for base class or for my class. One such example
is TreeView, which inconsistently raises Click event - from my point of
view, of course.
"Peter Duniho" <NpOeStPeAdM@xxxxxxxxxxxxxxxx> wrote in message
news:op.trocmtzu8jd0ej@xxxxxxxxxxxxxxxxxxxxxxx
Okay, this has come up in the past but I haven't seen anything that I
would call a good, solid general rule.

The question applies more generally, but I'm going to ask in the context
of a Control. How does one decide whether to write an "override" method
to an event and when to simply add an event handler for that event?

For example, you can either write a new OnPaint override, or you can
register a delegate that the Control's base OnPaint method will execute.

My current thought is that when you are not inheriting from the base
class, you register a delegate (duh, since you obviously have no way to
override the method in that case), and when you are inheriting from the
base class, you override the method (the main reason here being that if
you're inheriting from the base class, you want your new functionality to
always work, rather than relying on your delegate remaining in the event
handler list).

In other words, override when you can, add an event handler if you can't.

Is it really as simple as that? Or are there other factors I should be
considering?

Many thanks in advance for the wisdom of those who have gone down this
road before me. :)

Pete



.



Relevant Pages

  • Overriding "OnXXX" versus adding event handler
    ... How does one decide whether to write an "override" method to an event and when to simply add an event handler for that event? ... For example, you can either write a new OnPaint override, or you can register a delegate that the Control's base OnPaint method will execute. ... My current thought is that when you are not inheriting from the base class, you register a delegate, and when you are inheriting from the base class, you override the method. ...
    (microsoft.public.dotnet.framework)
  • Re: Call to Base-Class Does Not Succeed
    ... static void Main ... If you are handling this throug events as you are, then there is no need to call the base class as that has already happened by the time you get to your event handler. ... If instead of using event handlers you had overriden the OnMouseEnter method of the base class, then in your override you would want to make sure you called the base. ...
    (microsoft.public.dotnet.languages.csharp)
  • Dumb inheritance question:
    ... I'm subclassing the .NET WinForms ComboBox. ... preferred way of handling custom events is to override the event handler. ... combobox should not have the old KeyPress functionality, ...
    (microsoft.public.dotnet.framework.windowsforms.controls)
  • Re: Overriding "OnXXX" versus adding event handler
    ... if you use delegate, you've got hard to track bug, where you got to run the debugger to realize some forgotten event handler have some annoying code... ... How does one decide whether to write an "override" method to an event and when to simply add an event handler for that event? ... For example, you can either write a new OnPaint override, or you can register a delegate that the Control's base OnPaint method will execute. ... My current thought is that when you are not inheriting from the base class, you register a delegate, and when you are inheriting from the base class, you override the method. ...
    (microsoft.public.dotnet.framework)
  • Re: What can I store in LPARAM
    ... I'm not saying you're wrong, but with my call back up to the base class, ... may choose to override it completely, or by adding code before or after ... comments below regarding leaf. ... This means that a subclass can't change the method body behind ...
    (microsoft.public.vc.mfc)