Why event handler first parameters are incorrect

  • Thread starter Thread starter Andrus
  • Start date Start date
A

Andrus

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.
 
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
 
Andrus said:
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.
 
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.
 
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.
 
Andrus said:
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?
 
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).


....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top