473,587 Members | 2,505 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_KeyP ress(object sender, KeyPressEventAr gs e)
private void textBoxAge_Vali dating(object sender, CancelEventArgs e)
private void textBox_TextCha nged(object sender, EventArgs e)

//Tony
Jun 27 '08 #1
3 1365
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*********@nn owslpianmk.coms krev 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
3119
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 field and an event that is fired if the boolean field value changes. The derived SortedList class has an override for the Add method. The Add...
6
28124
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 RowDataBound event to set the commandargument of the delete button. The event is being fired and works fine. When I press the delete button, the...
16
2500
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 of properties or assign event handlers. I need to be able to clone the manipulated control at runtime. I could use the Clone method of some...
14
8600
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 handlers are set like the old one. Is there a way to do this job? For example, is there a way to use EventInfo object to get all event handlers of...
8
4258
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 raise an event how do I create an handler to catch these raised events. //Tony
0
7923
marktang
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8216
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. ...
0
8349
jinu1996
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7974
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8221
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...
0
6629
agi2029
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3845
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...
1
2364
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
0
1192
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...

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.