Exact difference between OnXXX() and their corresponding event handler?

  • Thread starter Thread starter Buddhist.CHinA
  • Start date Start date
B

Buddhist.CHinA

I believe there must be some difference between them, such as,
which has higher performance?
how to implement these OnXXX() by MS?
how to determine which one is preferred in a specific case?
....


Can someone give a summary? That must be very appreciated.
 
I believe there must be some difference between them, such as,
which has higher performance?
how to implement these OnXXX() by MS?
how to determine which one is preferred in a specific case?

The OnXXX is simply a method that raises an event and optionally perfoms
additional processing before or after raising the event. So, for example,
if you've got an event called Validated in your class, if you want to
implement your class in the Microsoft way, the OnValidated method would
look like that:

protected void OnValidated()
{
if (Validated != null)
{
Validated(/* event params here */
}
}

Then, whenever you need to raise the Validated event from somewhere in your
class, simply call OnValidated().

The OnXXX serves several purposes:

1) making it easier for methods in your class to raise an event: instead of
testing if somebody has registered to the event and then raise it, simply
call OnXXX

2) Performs some pre- or post-event processing. E.g., if you've got a
m_validated boolean that you want to set to true whenever something has
been validated, instead of setting it in every method that raises the
Validated event, do it in the OnValidated method:

protected void OnValidated()
{
m_validated = true;
if (Validated != null)
{
Validated(/* event params here */
}
}

3) Allow derived classes to control the event flow and pre- or post-event
processing. For example, if one of the derived class doesn't want the
Validated event to be raised, it can override the OnValidated() method and
do nothing (not such a good idea as the OnValidated method might do more
than just raise the event). Generally though, this is used to perform
additional pre- or post-processing, often used in Control derived classes
to do some custom painting in the OnPaint method. I'm not being very clear
here so have a look at Bob Powell's explanation here which is far better
that mine:
<http://www.bobpowell.net/overrideorhandle.htm>
 
Thx Mehdi. It's very clear.

In some cases (a sealed class, and ?), they can be identical. But
OnXXX() should be faster than evnet handlers, because of cost of
delegate implementation.
 
Still one question.
I didn't quite understand your saying "not such a good idea as the
OnValidated method might do more than just raise the event". Can you
explain it more?

thx.
 
Still one question.
I didn't quite understand your saying "not such a good idea as the
OnValidated method might do more than just raise the event". Can you
explain it more?

..NET documentation suggests that you always call the base.OnXXX() method in
your overrides because the base method may be more than simply an empty
virtual method, i.e., it may do some necessary processing such as setting a
state flag, etc. Hence, omitting the call to the base method may result in
undesirable side effects.
 
Mini-Tools Timm said:
.NET documentation suggests that you always call the base.OnXXX() method in
your overrides because the base method may be more than simply an empty
virtual method, i.e., it may do some necessary processing such as setting a
state flag, etc. Hence, omitting the call to the base method may result in
undesirable side effects.

Specifically, base method is responsible for calling other event
subscribers. This gives you flexibility of not breaking the event chain.
If your event handler throws an exception, the event subscriber
chain gets broken, noone else subscribed to the event after you will
get it. Overriding the virtual method gives you two things: you get
this event first, and calling base method version you continue the
chain (if desired).
 
Back
Top