By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,336 Members | 1,293 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,336 IT Pros & Developers. It's quick & easy.

Override vs Events

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
"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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.