473,230 Members | 1,521 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,230 software developers and data experts.

about event handlers

Hello!

Is it the normal procedure in C# and .NET framework to always use the actual
event object which is
passed as the second parameters to the event handler.

All of them are derived from the base class which is EventArgs so because of
this the second parameters could in all cases be EventArgs which had be be
cased
in the most cases to the the actual referenced event argument object before
accessing the referenced object.

Here I have some examples
private void textBoxAge_KeyPress(object sender, KeyPressEventArgs e)
private void textBoxAge_Validating(object sender, CancelEventArgs e)
private void textBox_TextChanged(object sender, EventArgs e)

//Tony
Jun 27 '08 #1
3 1353
On Tue, 24 Jun 2008 23:51:03 -0700, Tony <jo*****************@telia.com>
wrote:
Is it the normal procedure in C# and .NET framework to always use the
actual
event object which is
passed as the second parameters to the event handler.

All of them are derived from the base class which is EventArgs so
because of
this the second parameters could in all cases be EventArgs which had be
be
cased
in the most cases to the the actual referenced event argument object
before
accessing the referenced object.
Typos notwithstanding, you've answered your own question. :)

Most of the time, you want the data that's in that EventArgs sub-class.
It wouldn't make any sense to declare your event handler method to take
EventArgs instead, only to have to cast it before you use it.

So, yes...I feel it's "the normal procedure" to always use the actual
type, rather than the base type.

I suppose that for an event handler where you're going to ignore that
parameter, you could declare the argument as just the base class. In
fact, that might help document the fact that the handler method is
ignoring the parameter. But in that scenario, you might want to ask
yourself why it is you're handling the event in the first place. :)
(That's for events that actually do sub-class the EventArgs; obviously for
events that don't, you'd use EventArgs and that'd be perfectly normal).

Pete
Jun 27 '08 #2
Hello!

One more thing assume I create an event and an object derived from the
eventArgs containing some info about the
event.
Then create another event also with an object derived from the eventArgs
containing some info about the
event.

When creating the event handler I have two choices either use EventArgs as
the second parametr and cast to the actual type
or
use two separate event handler with the actual type as the second paramer.

So does it exist some recommended guidline to which one to use.
I would say use different event handler unles they they are logically
connected in some way.

Give me some comment about which one to use according to OOP.

//Tony

"Peter Duniho" <Np*********@nnowslpianmk.comskrev i meddelandet
news:op***************@petes-computer.local...
On Tue, 24 Jun 2008 23:51:03 -0700, Tony <jo*****************@telia.com>
wrote:
Is it the normal procedure in C# and .NET framework to always use the
actual
event object which is
passed as the second parameters to the event handler.

All of them are derived from the base class which is EventArgs so
because of
this the second parameters could in all cases be EventArgs which had be
be
cased
in the most cases to the the actual referenced event argument object
before
accessing the referenced object.

Typos notwithstanding, you've answered your own question. :)

Most of the time, you want the data that's in that EventArgs sub-class.
It wouldn't make any sense to declare your event handler method to take
EventArgs instead, only to have to cast it before you use it.

So, yes...I feel it's "the normal procedure" to always use the actual
type, rather than the base type.

I suppose that for an event handler where you're going to ignore that
parameter, you could declare the argument as just the base class. In
fact, that might help document the fact that the handler method is
ignoring the parameter. But in that scenario, you might want to ask
yourself why it is you're handling the event in the first place. :)
(That's for events that actually do sub-class the EventArgs; obviously for
events that don't, you'd use EventArgs and that'd be perfectly normal).

Pete

Jun 27 '08 #3
On Wed, 25 Jun 2008 00:34:36 -0700, Tony <jo*****************@telia.com>
wrote:
One more thing assume I create an event and an object derived from the
eventArgs containing some info about the event.
Then create another event also with an object derived from the eventArgs
containing some info about the event.

When creating the event handler I have two choices either use EventArgs
as
the second parametr and cast to the actual type or
use two separate event handler with the actual type as the second
paramer.
If the processing of the event is so similar as to justify using the same
handler method for both, then the event should almost certainly be using
the same EventArgs sub-class.
So does it exist some recommended guidline to which one to use.
I would say use different event handler unles they they are logically
connected in some way.
Yes, I try not to reuse the same method for operations that are _not_
logically connected in some way. :)

(In fact, as I've noted many times before in this newsgroup, I don't even
like having one method doing two disparate things even if those two things
do in fact happen to be related to each other in some logical way...see my
repeated objections to MSDN's approach to methods using Invoke() and
InvokeRequired, for example).

Pete
Jun 27 '08 #4

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

Similar topics

8
by: Dennis C. Drumm | last post by:
I have a class derived from a SortedList called SystemList that contains a list of objects indexed with a string value. The definition of the objects contained in the SortedList have a boolean...
6
by: Kevin Attard | last post by:
I am using a GridView inside a UserControl which has a template column for deleting the rows. Before databinding the gridview i am attaching the RowCommand and RowDataBound event. I am using the...
16
by: Hamed | last post by:
Hello I am developing a utility to be reused in other programs. It I have an object of type Control (a TextBox, ComboBox, etc.) that other programmers use it in applications. they may set some...
14
by: Hamed | last post by:
Hello It seems that I should implement ICloneable to implement my own clone object. the critical point for me is to make a control object based on another control object that all of its event...
8
by: Tony Johansson | last post by:
Hello! I wonder can somebody explain when is it suitable to use these methods OnKeyUp, OnKeyDown and OnKeyPress because these raise an event. These are located in class UserControl. If these...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.