[Rule Proposal] Declare event handlers correctly

58 views
Skip to first unread message

Néstor Salceda

unread,
Aug 16, 2008, 3:31:30 PM8/16/08
to Mono-Soc-2008
Title: Declare event handlers correctly

Description: There are some delegates used as event handler which
haven't the correct signature, bad returning type or parameter names.

References: http://msdn.microsoft.com/en-us/library/ms182133(VS.80).aspx
http://blogs.msdn.com/fxcop/archive/2007/08/09/what-rules-do-microsoft-have-turned-on-internally.aspx

Good example:

public delegate void BarHandler (object sender, EventArgs e);

public class Foo {
public event BarHandler OnBar;
}

Bad example:

public delegate int BarHandler (object obj, EventArgs args);

public class Foo {
public event BarHandler OnBar;
}

Well, this will be the latest rule and I would like to explore new ways
of checking. I have never wrote a rule for events, and I think new
problems could be found with the events (and also new rules written).

Comments?

Néstor.

Michael Hutchinson

unread,
Aug 16, 2008, 3:53:03 PM8/16/08
to mono-s...@googlegroups.com

This happens to be a point where I disagree with the FDG -- I prefer
the use of (sender, args) instead of (sender, e). However, I guess the
rule only checks in the delegate, where it's not such a big deal for
me. I can go on "misnaming" it in my actual event handler methods :-)

--
Michael Hutchinson
http://mjhutchinson.com

Néstor Salceda

unread,
Aug 16, 2008, 4:04:11 PM8/16/08
to mono-s...@googlegroups.com
Hey Michael,

El sáb, 16-08-2008 a las 15:53 -0400, Michael Hutchinson escribió:
> This happens to be a point where I disagree with the FDG -- I prefer
> the use of (sender, args) instead of (sender, e). However, I guess the
> rule only checks in the delegate, where it's not such a big deal for
> me. I can go on "misnaming" it in my actual event handler methods :-)

Yes, I agree with you. I also prefer name the second parameter as
args, because is more descriptive than e.

Anyways, the parameter names violation, won't have a high severity.
And if someone more agree, I can add "args" as a valid name too.

Thanks for your feedback Michael :)
Néstor.

Sebastien Pouliot

unread,
Aug 17, 2008, 10:35:27 AM8/17/08
to mono-s...@googlegroups.com
Hola Nestor,

Make sure you commit everything you have (even if not 100% complete /
reviewed) before GSoC deadline!

August 18:
~12 noon PDT / 19:00 UTC Firm 'pencils down' date. Mentors, students and
organization administrators can begin submitting final evaluations to
Google.

Sebastien

Sebastien Pouliot

unread,
Aug 17, 2008, 10:40:55 AM8/17/08
to mono-s...@googlegroups.com, gendarme

or you can:
(a) turn off this rule (e.g. your own rules.config)

but that's not very interesting (to me ;-) unless you also do the next
two steps

(b) create your own rule (by inheriting from this one) and override the
parameter check

(c) add this new rule/assembly to your own rules.config

Sebastien

Reply all
Reply to author
Forward
0 new messages