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 18 4794
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**************@TK2MSFTNGP10.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
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(PaintEventArgs 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
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**@isannoying.com> wrote in message
news:uo**************@TK2MSFTNGP10.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**************@TK2MSFTNGP10.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
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.googlegr oups.com... http://www.bobpowell.net/overrideorhandle.htm
Peace, --Carl
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**************@TK2MSFTNGP14.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.googlegr oups.com... http://www.bobpowell.net/overrideorhandle.htm
Peace, --Carl
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.
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******@NOSPAMdevelop.com> wrote in
message news:uE**************@TK2MSFTNGP10.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.
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**************@TK2MSFTNGP11.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******@NOSPAMdevelop.com> wrote in message news:uE**************@TK2MSFTNGP10.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.
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.
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******@NOSPAMdevelop.com> wrote in
message news:O4**************@tk2msftngp13.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.
> 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
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******@NOSPAMdevelop.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(PaintEventArgs e) { // Do your custom painting here
base.OnPaint(e); // this fires thte event handlers }
-- http://www.kynosarges.de
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**************@TK2MSFTNGP09.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
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**@isannoying.com> wrote in message
news:eB**************@TK2MSFTNGP11.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**************@TK2MSFTNGP09.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
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
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******@NOSPAMdevelop.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(PaintEventArgs 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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
{
...
}
|
by: Karsten Schramm |
last post by:
Hi,
when I run the following code:
using System;
namespace ConsoleApplication22
{
class Program
{
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
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,...
|
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...
|
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...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
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...
| |