473,320 Members | 1,969 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Override vs Events

Any reason why I should use one of these over the other?

Overriding:

protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);
// etc.
}
Events:

this.MouseDown += new MouseEventHandler(thisMouseDown);
public void thisMouseDown(object sender, MouseEventArgs e)
{
// etc.
}

--
Daisy The Cow
Now playing: Dannii Minogue - I Begin To Wonder
Nov 13 '05 #1
4 8094
Yes, there is a difference.

In the first one, you are overriding the method that causes the event. Thus,
no other methods that are registered as handling this event have fired (many
objects can register to execute methods from the same event). So this method
executes before the event fires, and if you cann the base class's
implementation, that will cause the event to fire, and for the registered
methods to fire.

In the second one, you are simply one of the objects who have registered to
receive the event once it fires, and to execute a particular method.

"Daisy" <da***@nospam.oops> wrote in message
news:be**********@linux01.dannytuppeny.com...
Any reason why I should use one of these over the other?

Overriding:

protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);
// etc.
}
Events:

this.MouseDown += new MouseEventHandler(thisMouseDown);
public void thisMouseDown(object sender, MouseEventArgs e)
{
// etc.
}

--
Daisy The Cow
Now playing: Dannii Minogue - I Begin To Wonder

Nov 13 '05 #2
"Marina" <zl*******@nospam.hotmail.com> wrote in message
news:uK**************@tk2msftngp13.phx.gbl...
Yes, there is a difference.

In the first one, you are overriding the method that causes the event. Thus, no other methods that are registered as handling this event have fired (many objects can register to execute methods from the same event). So this method executes before the event fires, and if you cann the base class's
implementation, that will cause the event to fire, and for the registered
methods to fire.

In the second one, you are simply one of the objects who have registered to receive the event once it fires, and to execute a particular method.


I understand the differences, but it appears I can make both do what the
other does. I was wondering if there are any advantages or considerations
for doing it one way over the other. Eg. Any difference in registering for
the MouseDown event and overriding it and calling the base method?
--
Daisy The Cow
Now playing: Lasgo - Alone
Nov 13 '05 #3
Yes, in some cases you have to override. Especially when you want to
suppress the action that Windows or C# will take when that event is fired.
Or if you want to suppress it and replace it with another action. You can't
do that with an event.

"Daisy" <da***@nospam.oops> wrote in message
news:be**********@linux01.dannytuppeny.com...
"Marina" <zl*******@nospam.hotmail.com> wrote in message
news:uK**************@tk2msftngp13.phx.gbl...
Yes, there is a difference.

In the first one, you are overriding the method that causes the event.

Thus,
no other methods that are registered as handling this event have fired

(many
objects can register to execute methods from the same event). So this

method
executes before the event fires, and if you cann the base class's
implementation, that will cause the event to fire, and for the registered methods to fire.

In the second one, you are simply one of the objects who have registered

to
receive the event once it fires, and to execute a particular method.


I understand the differences, but it appears I can make both do what the
other does. I was wondering if there are any advantages or considerations
for doing it one way over the other. Eg. Any difference in registering for
the MouseDown event and overriding it and calling the base method?
--
Daisy The Cow
Now playing: Lasgo - Alone

Nov 13 '05 #4
Yes, basically what Allen said.

If you are interested in only doing something in response to an event, then
just register for the event.

If you need to change what happens when the event is raised: such as cancel
it, or perform some action right before or right after the event (and all
the methods registered for it)fires, then override the method.

"Daisy" <da***@nospam.oops> wrote in message
news:be**********@linux01.dannytuppeny.com...
"Marina" <zl*******@nospam.hotmail.com> wrote in message
news:uK**************@tk2msftngp13.phx.gbl...
Yes, there is a difference.

In the first one, you are overriding the method that causes the event.

Thus,
no other methods that are registered as handling this event have fired

(many
objects can register to execute methods from the same event). So this

method
executes before the event fires, and if you cann the base class's
implementation, that will cause the event to fire, and for the registered methods to fire.

In the second one, you are simply one of the objects who have registered

to
receive the event once it fires, and to execute a particular method.


I understand the differences, but it appears I can make both do what the
other does. I was wondering if there are any advantages or considerations
for doing it one way over the other. Eg. Any difference in registering for
the MouseDown event and overriding it and calling the base method?
--
Daisy The Cow
Now playing: Lasgo - Alone

Nov 13 '05 #5

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

Similar topics

14
by: JPRoot | last post by:
Hi I use the following syntax to have events inherited from base to child classes which works nicely (virtual and override keyword on events). But I am wondering if it is a "supported" way of using...
8
by: JPRoot | last post by:
Hi M. Jeffrey Tan, Just hopping you didn't forget me? :) Thanks JPRoot ----- \"Jeffrey Tan\" wrote: -----
2
by: ~toki | last post by:
How can i take the control of the key events in Class2 ? This is the code snipped that i'd tried (after try some others): public class Main : System.Windows.Forms.Form { protected virtual...
7
by: Dave Y | last post by:
I am a newbie to C# and am having trouble trying to override a ListView property method. I have created a new class derived from the Forms.Listview and I cannot figure out the syntax to override...
18
by: bhavin | last post by:
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...
8
by: Keith Smith | last post by:
Could some one help me a little? I am trying to understand when/where you would ever want to use "protected override..." code such as this. How is this any different from creating a...
1
by: Steve | last post by:
I would like to override the IsInputKey function in order to trap the arrow up/down keys in the Key Events. I understand that arrow keys don't have a char representation and thus, hard to trap. ...
3
by: mtczx232 | last post by:
in previous versions we can put Override header method into code by choosing Override from upper left dropdown list, and choosing the method from right one. but in VS2005 Override Option...
5
by: Marcel Hug | last post by:
Hi NG ! I'm new in C# and I'm reading a book about the fundamentals and concepts. In the chapter Methods it's written to use virtual, if i would like to override the method in a subclass. This...
4
by: Rob | last post by:
I've constructed a user control inherited from ListView so I can handle and respond to scrolling events (to keep 2 listviews scrolling in sync). My user control includes an Overrides of WndProc...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.