Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / June 2006

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Buddhist.CHinA@gmail.com - 25 Jun 2006 10:37 GMT
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.
Mehdi - 25 Jun 2006 12:08 GMT
> 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>
Buddhist.CHinA@gmail.com - 26 Jun 2006 04:11 GMT
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.

> > I believe there must be some difference between them, such as,
> > which has higher performance?
[quoted text clipped - 48 lines]
> that mine:
> <http://www.bobpowell.net/overrideorhandle.htm>
Buddhist.CHinA@gmail.com - 26 Jun 2006 04:21 GMT
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.

> > I believe there must be some difference between them, such as,
> > which has higher performance?
[quoted text clipped - 48 lines]
> that mine:
> <http://www.bobpowell.net/overrideorhandle.htm>
Mini-Tools Timm - 27 Jun 2006 15:07 GMT
> 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.

Signature

Timm Martin
Mini-Tools
.NET Components and Windows Software
http://www.mini-tools.com

Sericinus hunter - 27 Jun 2006 15:39 GMT
>> Still one question.
>> I didn't quite understand your saying "not such a good idea as the
[quoted text clipped - 6 lines]
> 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).

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.