Calling methods or handling events in derived classes.

  • Thread starter Thread starter Mythran
  • Start date Start date
M

Mythran

I once read an article somewhere describing why we should not override the
OnXXX methods of classes when you want to extend the functionality of a
class. It stated that you should handle the event instead (inside the
derived class). Does anyone know of this article or an article that
discusses this or the opposite (should override OnXXX instead of handling
the event)?

Thanks,
Mythran
 
Mythran,

The only case I can see doing this is if you are intending to extend the
class for a specific purpose and what you are doing does not coincide with
the intention of the event. However, if you are deriving from a class, and
what you are doing in the OnXXX override coincides with what you want the
derivations of your class to do, then I don't see why you shouldn't do it.

Hope this helps.
 
Hi,


Mythran said:
I once read an article somewhere describing why we should not override the
OnXXX methods of classes when you want to extend the functionality of a
class. It stated that you should handle the event instead (inside the
derived class). Does anyone know of this article or an article that
discusses this or the opposite (should override OnXXX instead of handling
the event)?

I'm not sure of that, IMO there is a reason why the OnXXX are marked as
protected virtual and that's cause it's expected that you have the need to
extend them.

In MSDN you find:
The OnXXX method also allows derived classes to handle the event without
attaching a delegate. This is the preferred technique for handling the event
in a derived class.
 
Ignacio Machin ( .NET/ C# MVP ) said:
Hi,




I'm not sure of that, IMO there is a reason why the OnXXX are marked as
protected virtual and that's cause it's expected that you have the need to
extend them.

In MSDN you find:
The OnXXX method also allows derived classes to handle the event without
attaching a delegate. This is the preferred technique for handling the
event in a derived class.

Hmm, I understand this and I have always overrode the OnXXX methods instead
of handling the events. I know I read something somewhere that says you
should not do this (at least in WebControls???) and instead handle the
event....but I can't remember where or the exact instance on when you should
not use the OnXXX methods...if I find it, I'll post it.

Thanks Nicholas and Ignacio,
Mythran
 
Hi,

Mythran said:
Hmm, I understand this and I have always overrode the OnXXX methods
instead of handling the events. I know I read something somewhere that
says you should not do this (at least in WebControls???) and instead
handle the event....but I can't remember where or the exact instance on
when you should not use the OnXXX methods...if I find it, I'll post it.

Thanks Nicholas and Ignacio,
Mythran

In theory I see no difference between win and web controls. IMO the events
are more intended to the "outside world". as it's the only way you can
inform the external instances of a change in the status of the control
 
Back
Top