473,756 Members | 5,660 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
18 4848
Thanks Jay for the tips and the links too.
Speed is not of much concern for me right now as much as common/best
practice is, so I think I will go the override way.
Just wanted to be sure i do it the more common way and not just develop some
code which works.. somehow. Also if i have a reason to something one way out
of 2 possible, i will be more consistent. Else i'll use one and then the
other in another case just because.

Thanks a ton again.
-bhavin

"Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> wrote in message
news:O4******** ******@TK2MSFTN GP11.phx.gbl...
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 #11
I mean that by overriding you can layer code in before or after the event handers run with confidence that you are in control of the ordering

Regards

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

Richard,
The crucial thing is the ability to layer in code relative to event
firing.

I'm not specifically sure what you mean here.
Nov 16 '05 #12
Richard,
Yes I thought that was what meant after I read my posted response to you.

For some reason it did not occur to me when I was composing my response...

Thanks
Jay

"Richard Blewett [DevelopMentor]" <ri******@NOSPA Mdevelop.com> wrote in
message news:O4******** ******@tk2msftn gp13.phx.gbl...
I mean that by overriding you can layer code in before or after the event
handers run with confidence that you are in control of the ordering

Regards

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

Richard,
The crucial thing is the ability to layer in code relative to event
firing.

I'm not specifically sure what you mean here.

Nov 16 '05 #13
> 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?


I generally agree with the statement.
However, I am not sure about Forms.
What would you do with a Form?
When you write a code for a form, actually you define a class (like class
Form1).
Would you use Paint handler or override OnPaint method?
Sam
Nov 16 '05 #14
That should be the other way round, I think: first call base.OnPaint,
then add your custom paint code. Otherwise, whatever you painted
might get overwritten by the base call.

On Tue, 04 Jan 2005 10:00:51 -0800, "Richard Blewett [DevelopMentor]"
<ri******@NOSPA Mdevelop.com> wrote:
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
}

--
http://www.kynosarges.de
Nov 16 '05 #15
I'm not quite sure why they decided to make paint an event... but probably
so that other components can hook into the paint of the form and 'paint on
top'. It at least makes it possible to have more than one painter of a form.

--
John Wood
Blog: http://spaces.msn.com/members/johnwood/
"Sam Kong" <su***********@ hotmail.com> wrote in message
news:e9******** ******@TK2MSFTN GP09.phx.gbl...
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?


I generally agree with the statement.
However, I am not sure about Forms.
What would you do with a Form?
When you write a code for a form, actually you define a class (like class
Form1).
Would you use Paint handler or override OnPaint method?
Sam

Nov 16 '05 #16
My guess would be so that it could be accessed via designers - drop a
control on a form and add code to its Paint event rather than having to :
- derive a control
- override the OnPaint method
- add the new control to your form programatically
OR
- create a new control library containing your new control so that the
new control could be dropped on a form
"John Wood" <sp**@isannoyin g.com> wrote in message
news:eB******** ******@TK2MSFTN GP11.phx.gbl...
I'm not quite sure why they decided to make paint an event... but probably
so that other components can hook into the paint of the form and 'paint on
top'. It at least makes it possible to have more than one painter of a form.
--
John Wood
Blog: http://spaces.msn.com/members/johnwood/
"Sam Kong" <su***********@ hotmail.com> wrote in message
news:e9******** ******@TK2MSFTN GP09.phx.gbl...
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?


I generally agree with the statement.
However, I am not sure about Forms.
What would you do with a Form?
When you write a code for a form, actually you define a class (like class Form1).
Would you use Paint handler or override OnPaint method?
Sam


Nov 16 '05 #17
I'm sure I posted this earlier in the thread, but the main reason for having a paint event is to allow consumers of your control to "decorate" its appearance with their own custom drawing

Regards

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

My guess would be so that it could be accessed via designers - drop a
control on a form and add code to its Paint event rather than having to :
- derive a control
- override the OnPaint method
- add the new control to your form programatically
OR
- create a new control library containing your new control so that the
new control could be dropped on a form

Nov 16 '05 #18
Christoph Nahr wrote:
That should be the other way round, I think: first call base.OnPaint,
then add your custom paint code. Otherwise, whatever you painted
might get overwritten by the base call.

On Tue, 04 Jan 2005 10:00:51 -0800, "Richard Blewett [DevelopMentor]"
<ri******@NOSPA Mdevelop.com> wrote:

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
}


I partially agree.
In this case (OnPaint), the base implementation should be called first
as it does some sort of default painting, which obviously should be done
first. But after the default painting, the hooked-up event handlers are
invoked to draw the previously-so-called decorations, eventually
followed by the overriding implementation( s) for yet another batch of
customized drawing which may *overpaint* our precious decorations.
So actually, no solution is better.
The order of execution is key in this situation. But in most other
cases, the order of invocation is not that important, as long as the
handlers (and overriding implementations ) *eventually* do get invoked.

Cheers,

Benoit
Nov 16 '05 #19

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

Similar topics

5
5332
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
11581
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
3516
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
2448
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
1704
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
1612
by: Karsten Schramm | last post by:
Hi, when I run the following code: using System; namespace ConsoleApplication22 { class Program {
0
10069
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
9735
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7285
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
6556
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
5168
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...
0
5324
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3828
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
3395
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2697
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.