473,729 Members | 2,142 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

event vs method override

Hi,
Can someone point me to some good best practices kind of documentation on
use of events compared to method overriding.
Ex. In Windows Forms when should i have an event handler for Paint, and when
should i override OnPaint?

I have to implement added functionality in a child class, and am in two
minds .. to have the parent fire an event, or make the method virtual and
override in the child. Am hoping some reading with description of various
scenerios will increase my understanding and eventually help me make the
decision.

thanx in advance
-bh
Nov 16 '05 #1
18 4843
I can't think of a document, sorry.
But I can give you my opinion.

Choosing to use an override forces your user to inherit from the class, so
only choose override as an option when you'd be expecting the user to
customize the behavior of your class (which itself isn't generally a very
common thing) by modifying what would normally be implementation details of
your class.

Choose events when you want to notify of something so they can potentially
go off and do something else, consider situations where more than one object
may subscribe to the event, something which isn't possible with overrides.

--
John Wood
Blog: http://spaces.msn.com/members/johnwood/
"bhavin" <bh@somecom.com > wrote in message
news:eS******** ******@TK2MSFTN GP10.phx.gbl...
Hi,
Can someone point me to some good best practices kind of documentation on
use of events compared to method overriding.
Ex. In Windows Forms when should i have an event handler for Paint, and when should i override OnPaint?

I have to implement added functionality in a child class, and am in two
minds .. to have the parent fire an event, or make the method virtual and
override in the child. Am hoping some reading with description of various
scenerios will increase my understanding and eventually help me make the
decision.

thanx in advance
-bh

Nov 16 '05 #2
There are two crucial differences between overriding and adding an event handler

1) To override you need to be a derived class (obviously) whereas anyone can add an event handler to a control.

2) The base class version of these virtual methods fires the event handlers. So if you need to layer in code in a specific place relative to when the event handlers fire (before or after) overriding is the only sure-fire way of doing this. The Paint event is a great example of this. Normally, people add a paint event handler to "decorate" your control from its standard appearance. You need to make sure the control paints it's appearance before the event handlers run otherwise you will draw over the top of the decorations. So the folllowing is the standard pattern for a custom control

protected override void OnPaint(PaintEv entArgs e)
{
// Do your custom painting here

base.OnPaint(e) ; // this fires thte event handlers
}

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Hi,
Can someone point me to some good best practices kind of documentation on
use of events compared to method overriding.
Ex. In Windows Forms when should i have an event handler for Paint, and when
should i override OnPaint?

I have to implement added functionality in a child class, and am in two
minds .. to have the parent fire an event, or make the method virtual and
override in the child. Am hoping some reading with description of various
scenerios will increase my understanding and eventually help me make the
decision.

thanx in advance
-bh

Nov 16 '05 #3
My client class IS a child of the main class. We are building a forms
framework, which acts as a core for the individual forms (which also will be
created by us). So the form class in the final form assembly is inherited
from the framework form class.

I was hoping to get various usecases which will help me decide, depending on
which usecase i can relate to.
Though it is a child class i am leaning towards using events for this
specific case.

-bh
"John Wood" <sp**@isannoyin g.com> wrote in message
news:uo******** ******@TK2MSFTN GP10.phx.gbl...
I can't think of a document, sorry.
But I can give you my opinion.

Choosing to use an override forces your user to inherit from the class, so
only choose override as an option when you'd be expecting the user to
customize the behavior of your class (which itself isn't generally a very
common thing) by modifying what would normally be implementation details of your class.

Choose events when you want to notify of something so they can potentially
go off and do something else, consider situations where more than one object may subscribe to the event, something which isn't possible with overrides.

--
John Wood
Blog: http://spaces.msn.com/members/johnwood/
"bhavin" <bh@somecom.com > wrote in message
news:eS******** ******@TK2MSFTN GP10.phx.gbl...
Hi,
Can someone point me to some good best practices kind of documentation on use of events compared to method overriding.
Ex. In Windows Forms when should i have an event handler for Paint, and

when
should i override OnPaint?

I have to implement added functionality in a child class, and am in two
minds .. to have the parent fire an event, or make the method virtual and override in the child. Am hoping some reading with description of various scenerios will increase my understanding and eventually help me make the
decision.

thanx in advance
-bh


Nov 16 '05 #6
Thanks Carl for the link.
Its an interesting article and Bob end it with
" Generally, if you are deriving from a control, always override. A control
should never handle it's own events because to do so can seriously effect
the rules of object-orientation. "
Would experts here maintain the same opinion about controls, and other
non-control inherited classes?
It kinda makes me think about my idea of using events in a child class to
extend more functionality.

-bh

<ca***********@ gmail.com> wrote in message
news:11******** **************@ c13g2000cwb.goo glegroups.com.. .
http://www.bobpowell.net/overrideorhandle.htm

Peace,
--Carl

Nov 16 '05 #7
bh,
I tend to view events as external to the class, they are used to notify
other objects that something interesting happened to this object.

While Overridable methods are internal to the class, they are used to notify
base or derived classes (the object itself) that something interesting
happened to the object (itself).

I don't have the specific reference on MSDN however I distinctly remember
reading that calling an overridable method will be faster then raising an
event.

The following mention using the override instead of the event:

http://msdn.microsoft.com/library/de...Guidelines.asp

http://msdn.microsoft.com/library/de...Guidelines.asp

The first link mentions "The purpose of the method is to provide a way for a
derived class to handle the event using an override. This is more natural
than using delegates in situations where the developer is creating a derived
class".

It may have been in this article, however I am not seeing it right now:
http://msdn.microsoft.com/library/de...l/scalenet.asp

Hope this helps
Jay

"bhavin" <bh@somecom.com > wrote in message
news:en******** ******@TK2MSFTN GP14.phx.gbl...
Thanks Carl for the link.
Its an interesting article and Bob end it with
" Generally, if you are deriving from a control, always override. A
control
should never handle it's own events because to do so can seriously effect
the rules of object-orientation. "
Would experts here maintain the same opinion about controls, and other
non-control inherited classes?
It kinda makes me think about my idea of using events in a child class to
extend more functionality.

-bh

<ca***********@ gmail.com> wrote in message
news:11******** **************@ c13g2000cwb.goo glegroups.com.. .
http://www.bobpowell.net/overrideorhandle.htm

Peace,
--Carl


Nov 16 '05 #8
In terms of Windows Forms controls the speed argument is a total red herring - how about a click event

1) there is a user involved - and so they are thinking
2) they decide to click the mouse - to actually do this will take a reasonable amount of time (milliseconds?)
3) there is a hardware interrupt as the mouse click is received
4) the mouse click has to make it from Kernel mode to User mode and be *posted* to the relevent windows message queue
5) the message pump picks up the message and dispatches it to the windows procedure

The small number of machine instructions difference between virtual and delegate based dispatch is lost in the noise. The crucial thing is the ability to layer in code relative to event firing.

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

I don't have the specific reference on MSDN however I distinctly remember
reading that calling an overridable method will be faster then raising an
event.

Nov 16 '05 #9
Richard,
The crucial thing is the ability to layer in code relative to event
firing. I'm not specifically sure what you mean here.
I agree the "speed" argument is largely a red herring, especially in the UI.
(the 80/20 rule).

However! My main point was not about speed, the main point I was attempting
to make was about internal verses external notification!

Although I did not mention I also agree with the design guidelines, using
the override for derived classes is "more natural", being "more natural" is
also more important then performance (again the 80/20 rule)...

Jay
"Richard Blewett [DevelopMentor]" <ri******@NOSPA Mdevelop.com> wrote in
message news:uE******** ******@TK2MSFTN GP10.phx.gbl... In terms of Windows Forms controls the speed argument is a total red
herring - how about a click event

1) there is a user involved - and so they are thinking
2) they decide to click the mouse - to actually do this will take a
reasonable amount of time (milliseconds?)
3) there is a hardware interrupt as the mouse click is received
4) the mouse click has to make it from Kernel mode to User mode and be
*posted* to the relevent windows message queue
5) the message pump picks up the message and dispatches it to the windows
procedure

The small number of machine instructions difference between virtual and
delegate based dispatch is lost in the noise. The crucial thing is the
ability to layer in code relative to event firing.

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

I don't have the specific reference on MSDN however I distinctly
remember
reading that calling an overridable method will be faster then raising an
event.

Nov 16 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
5331
by: Action | last post by:
does it works like ordinary virtual method?? coz I find that child class can't invoke the event of the parent class. class parent { public virtual event SomeDelegate SomeChanged; } class child : parent {
2
5160
by: Besta | last post by:
Hello all, I am having trouble creating a windows service with a timer. Everything seems to go ok but the elapsed event does not fire.Can anyone shed any light on this, may be something simple as I am new to this. Full code below : using System; using System.Collections; using System.ComponentModel;
1
11579
by: Earl Teigrob | last post by:
I did a ton of searching to try and find a simple solution to this issue and finally wrote my own, which I am sharing with everyone. In my searching, I did find a very complete and robust solution at http://weblogs.asp.net/asmith/archive/2003/09/15/27684.aspx but it was far more complex then I needed. (I got lost trying to figure it all out). Therefore, here goes my simple "web dialog box with parent event handler fireing" solution. ...
4
3513
by: John Boy | last post by:
Hi, Can anyone help. This is really doing my nut in. 3 years ASP exp. and now doing .DOT which is a step in the wrong direction. Basically I am left with the code of a guy who has left. When I click a button on a pop-up window the javascript for that button click does a 'button.form.submit'. On the Server side there is a Button click event for this button, but for some reason it no longer fires. It worked fine before and everything...
8
2445
by: Dave Wurtz | last post by:
All, Is there a way to force a derived class to contain an event. I have done this with methods (i.e. Protected MustOverride Sub Test), but can I do something similar with an event (Protected MustOverride Event MyEvent doesn't work for obvious reasons)? Thanks in advance. Dave
7
390
by: Charles Law | last post by:
I may have asked this before, but what is the purpose of both these functions? Is there a time when one should be called over the other? Does one supersede the other, or do they both have their time and place? TIA Charles
7
1703
by: tony | last post by:
Hello! What is the differens if I use event handler onSizeChanged compare to using the other event handler MeltPracForm_SizeChanged. I see both as event handler is that right? I catch the event in both cases. So is it more or less the same thing which of these two event handler I use. protected override void OnSizeChanged(EventArgs e) {
3
1816
by: polocar | last post by:
Hi, I'm writing a C# program (using Visual Studio 2005 Professional Edition). I have defined a class MyPanel in the following way: class MyPanel : Panel { ... }
8
1611
by: Karsten Schramm | last post by:
Hi, when I run the following code: using System; namespace ConsoleApplication22 { class Program {
0
9426
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9280
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9200
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6722
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6016
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4525
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3238
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2677
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2162
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.