tags:

views:

553

answers:

3

Hello,

When I add new event handler for any event, VS creates method like object_Click. But ReSharper underlines this method as Warning, because all methods should not have any delimeters such as "_".

How can I customize rules of ReSharper so that it doesn't underline such methods? Or may be I should rename such methods?

Thanks in advance.

A: 

On your file menu you should have "Resharper" Click it -> Options -> Naming conventions (on left menu).

From there you can specify what naming conventions are used for each of the naming types/styles.

Pat
Thanks, but ReSharper allows to add some rules like UpperCamelCase, lowerCamelCase, ALL_UPPER, all_lower, First_upper. But I need something like this - UpperCamelCase_UpperCamelCase.ReSharper 5 have "Advanced settings" but I can't write right rule.
Pavel Belousov
+2  A: 

Personally, I'd suggest renaming the methods. Generally I think VS comes up with terrible names for both controls and events.

I prefer to make a method name say what it does, not what calls it. That promotes reuse as well. Admittedly the signature of an event handler is often not ideal for reuse - I'd argue that often a lambda expression calling a method with more sensible parameters would be useful:

button.Click += (sender, args) => SaveCurrentDocument();

but obviously the designer doesn't support that :(

Of course, renaming all the methods is going to be more work than just changing the R# settings, if you can find some that work...

Jon Skeet
But the method in question IS doing what it says. It's handling the fact that a button was clicked. If the desired action is to save the document, then the button click handler should call some other method to do that. I dislike a lot of code in the event handlers for just this reason. It also gets in the way of wiring up multiple actions to the same result (button click, menu item, context menu item, etc). If each of these handlers call the same common method, then there's no confusion.
Mel
@Mel: But then why bother having the method? Wouldn't the lambda expression as per my example be clearer?
Jon Skeet
In most cases, I think it would. For an event such as clicking on a row within a grid, then I think the event handler should be responsible for interpreting the control-specific information such as determining which row was clicked, and perhaps extracting the bound object before passing it off to a well-named method that does something with that object. If this can be done within a lambda, then great. Sometimes it's nice to have it broken out in a "proper" event handler for clarity, though. As always... it depends.
Mel
+5  A: 

For C# (or VB), make the following change:

ReSharper | Options | Languages | C# | C# Naming Style, Advanced settings... Change 'Event subscriptions on fields' from '$object$On$event$' to '$object$$event$'.

You may also want to add additional rules to entity kinds like 'Types and namespaces' to account for code-generated classes such as 'Default'. For example, add a new rule with a '' Name Prefix and a Name Style 'UpperCamelCase'.

dnorthut
In my case it was changing $object$_On$event$ to $object$_$event$ (removing the 'On'). Otherwise, method names like Page_Load were still underlined.
Olaf