473,325 Members | 2,805 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,325 software developers and data experts.

Why event handler first parameters are incorrect

I noticed that DataGridView CellValidated() and other event handler first
parameter is object:

Grid.CellValidated+=new
DataGridViewCellEventHandler(Grid_CellValidated);
....

void Grid_CellValidated(object sender, DataGridViewCellEventArgs e)
{
.....
}

Why ? Correct signature must be

void Grid_CellValidated(DataGridView sender, DataGridViewCellEventArgs e) {

Andrus.

Jun 27 '08 #1
7 1803
jj
Hello Andrus

I do not have an answer to your question. But I'm curious: what is the
problem if sender is of the type System.Object? We both know that we
can easily cast.....maybe you want to avoid this casting?

Regards,
Jim
Jun 27 '08 #2
Andrus wrote:
I noticed that DataGridView CellValidated() and other event handler
first parameter is object:

Grid.CellValidated+=new
DataGridViewCellEventHandler(Grid_CellValidated);
...

void Grid_CellValidated(object sender, DataGridViewCellEventArgs
e) {
....
}

Why ? Correct signature must be

void Grid_CellValidated(DataGridView sender, DataGridViewCellEventArgs e) {

Andrus.
Event handler delegates follow a standard that says that:

1. argument #1 must be of type Object
2. argument #2 must descend from EventArgs

While the actual object being sent to your event handler implementation
is a DataGridView, the sender parameter still needs to be declared as
type Object.

--
Lasse Vågs¿ther Karlsen
mailto:la***@vkarlsen.no
http://presentationmode.blogspot.com/
PGP KeyID: 0xBCDEA2E3
Jun 27 '08 #3
I do not have an answer to your question. But I'm curious: what is the
problem if sender is of the type System.Object? We both know that we
can easily cast.....maybe you want to avoid this casting?
I have 2 issues with this:

1. It requires to use casting. Good coding style requires not to use casts.

2. I must create my own entity property events invoked from DataGridView
events if column is changed.

Is it OK to pass DataGridView as sender parameter and event arguments in
command line, without using Eventargs class ?
In some cases I do'nt need sender. Is it OK to create event without sender
parameter ?

Andrus.

Jun 27 '08 #4
Lasse,
Event handler delegates follow a standard that says that:

1. argument #1 must be of type Object
2. argument #2 must descend from EventArgs

While the actual object being sent to your event handler implementation
is a DataGridView, the sender parameter still needs to be declared as
type Object.
Where to find link to this standard ?
I havent seen any reference to it.
Why this standard exists ? I must use ugly casts due to this.
>I can only speak to what I've observed, and the reason for this is that it
is easier to re-use an event handler implementation for various controls,
since most of the difference is in the EventArgs-descendant anyway.
I'm planning to create my own event which are set by DataGridView if entity
property is changed and committed.
Is it OK to create event wothout sender and passing aruments as parameters,
without using EventArgs class ?
>I've seen it in an older video with Anders Hejlsberg where he talked about
the reasoning behind using EventArgs, and why Sender was type-less so to
speak, but I can't remember the actual link.
Only information I have found is from Jon's website.Jon does'nt describe it.
>And yes, you'll have to use a typecast. It's just the way it is so you'd
just better get used to it.
Andrus.

Jun 27 '08 #5
Andrus wrote:
>I do not have an answer to your question. But I'm curious: what is the
problem if sender is of the type System.Object? We both know that we
can easily cast.....maybe you want to avoid this casting?

I have 2 issues with this:

1. It requires to use casting. Good coding style requires not to use casts.

2. I must create my own entity property events invoked from DataGridView
events if column is changed.

Is it OK to pass DataGridView as sender parameter and event arguments in
command line, without using Eventargs class ?
In some cases I do'nt need sender. Is it OK to create event without
sender parameter ?

Andrus.
You can write event handler declarations with all sorts of arguments.
The Object/EventArgs standard is not enforced by the compiler.

The EventArgs part is for versioning though, easier to add more
properties to this class than it is to fix all the places where you need
to add more arguments later.

Also remember that if at some point you suddenly want sender, it will
require you to go back and fix all places where you use these events.
Why not just add the sender now and just ignore it?

--
Lasse Vågsæther Karlsen
mailto:la***@vkarlsen.no
http://presentationmode.blogspot.com/
PGP KeyID: 0xBCDEA2E3
Jun 27 '08 #6
On Mon, 26 May 2008 05:54:57 -0700, Andrus <ko********@hot.eewrote:
Where to find link to this standard ?
http://msdn.microsoft.com/en-us/library/ms229011.aspx
I havent seen any reference to it.
There is a prominent link to the above page from the main "Events (C#
Programming Guide)" page.
Why this standard exists ? I must use ugly casts due to this.
"Ugly" is in the eye of the beholder. Also, I doubt anyone here can tell
you precisely why the standard exists. But we can offer some possible
benefits as suggestions for why it might be that way:

* Avoids type proliferation. There's a delegate type for every event
signature. We already have a different delegate type for every different
event data argument type (EventArgs-derived classes). You can imagine the
number of types that would be required to support every combination of
possible sender and event data arguments.

* Typing the sender more narrowly than Object doesn't necessarily
remove the need to cast anyway. In the Forms namespace, for example, it's
not uncommon to have a single handler deal with events from multiple
objects of different types. Inasmuch as you need the actual type of the
sender (not always the case, but does happen), you'd wind up casting from
the given type anyway (if a "lowest-common-denominator" type was used,
like Control), or you would not be able to easily use the same handler
with multiple events (if the event was always declared to match the actual
type of the sender).

There may be other reasons, but IMHO the first of the above is quite
significant, and the second of the above effectively minimizes your claim
that one could avoid "ugly casts" in a practical way without sacrificing
existing benefits.
I'm planning to create my own event which are set by DataGridView if
entity property is changed and committed.
Is it OK to create event wothout sender and passing aruments as
parameters, without using EventArgs class ?
It depends on how you're going to use it. But yes, assuming your code is
the only code that will ever use the event, you can make it look any way
you like. Some binding mechanisms rely on your code following the .NET
convention, so if you care about your event and associated property being
useful in a binding scenario (not uncommon for a "property changed"
event), you should follow the .NET design standard.

Pete
Jun 27 '08 #7
The original question was about the signature of this statement:

Grid.CellValidated += new
DataGridViewCellEventHandler(Grid_CellValidated);

In the above statement, "DataGridViewCellEventHandler" is a delegate
whose sole purpose is to pass the NAME (i.e., really an address) of an
event handler to the list of registered methods to be called should
that event occur in the future. The above statement just adds the
address of "Grid_CellValidated" to the linked list of procedures which
will be called in the future when the "CellValidated" event gets
raised. Or in Java parlance, the statement above registers
"Grid_CellValidated" as a listener.

"Grid_CellValidated" is the actual event handler, and as such, it must
have the event handler signature (sender, Eventargs).
....

Jun 27 '08 #8

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

Similar topics

7
by: Pavils Jurjans | last post by:
Hallo, I have been programming for restricted environments where Internet Explorer is a standard, so I haven't stumbled upon this problem until now, when I need to write a DOM-compatible code. ...
4
by: Pai | last post by:
hello there, I am trying to rersize the window it works find in IE but doea not work with mozilla window.attachEvent(onload,MWSOnLoad); window.onload = function (MWSOnLoad) { alert('hello');...
0
by: Frank 'Olorin' Rizzi | last post by:
Hello everyone. This is quite convoluted, but I'll try to make it simple. I have a couple of bottom-line questions (I guess): 1~ what happens between the Page_Load routine in the code behind...
2
by: Fei | last post by:
HI, I have a problem about EventHandler. I have a form with one button on it. I implement Button's click event. Is there a way I can determine whether I implement a handler for this event at...
41
by: JohnR | last post by:
In it's simplest form, assume that I have created a usercontrol, WSToolBarButton that contains a button. I would like to eventually create copies of WSToolBarButton dynamically at run time based...
7
by: Michael D. Ober | last post by:
Is there anyway to raise an event from a class and require that any program using that class (not just inheritance) have an event handler for specific events in the class? In my case, some events...
5
by: james | last post by:
Hello, I am having a little trouble creating an event handler for a context menu toolstripmenuitem. I've seen various tutorials and so on, but I keep getting a bit stuck! So far I have a second...
3
by: Tony | last post by:
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...
24
by: =?Utf-8?B?U3dhcHB5?= | last post by:
Can anyone suggest me to pass more parameters other than two parameter for events like the following? Event: Onbutton_click(object sender, EventArgs e)" Event handler: button.Click += new...
0
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...
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: 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)...
1
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.