Event declartion and bracket issues.

Jun 30, 2010 at 10:44 AM
Edited Jun 30, 2010 at 11:08 AM
Hello, I recently started using StyleCop and now trying to make everything according to standards. I have for now one issue: Private event declaration rule. Basically my codding standard looks: private event CancelEventHandler _onCancel; //event declaration /// <summary> /// Adds or removes Cancel event. /// </summary> [Category("Action")] [Description("Cancel")] public event CancelEventHandler Cancel { add { _onCancel += value; } remove { _onCancel -= value; } } /// <summary> /// Fires cancel event. /// </summary> /// <param name="e"></param> protected virtual void OnCancel(CancelEventArgs e) { if (_onCancel != null) { _onCancel(this, e); } } Is there any way to use separate rule for private event declaration? Since it should be treated as variable declaration not event.
Jul 1, 2010 at 8:13 AM
Edited Jul 1, 2010 at 8:16 AM

Hi, msmolka

Could you clarify your question?
Is there any existing StyleCop rule that is violated by your code?
Or you are just requesting for a new rule that would be able to control style like you mentioned?

Best regards,
Oleg Shuruev

Jul 1, 2010 at 10:25 AM
Hi, There are 2 issues with 2 that: one move private events from declaration section to place after public once ( I can simply uncheck this rule... so no big deal). Other is with naming I need to suppress each event for naming check since there is no separate rule for private event naming.
Jul 2, 2010 at 10:32 AM


As you could already understand, original StyleCop's style (default set of enabled/disabled rules) differs from yours in this situation.
And it seems that you really don't need using an ordering rule (like SA1202).

With regard to the naming of events, I would recommend a look at StyleCop+ (http://stylecopplus.codeplex.com/).
Original StyleCop insists on limited naming conventions (actually there are two of them - PascalNaming and this.camelNamingPrefixedWithThis, no uderscores).

StyleCop+ offers you creating unlimited number of own conventions, and assigning them to different code entities - classes, methods, events, etc.
E.g., you could say that you want all events to be named like _sampleName.

The only bad thing about StyleCop+ is that it doesn't distinguish between private and non-private event handlers for now.
You can try StyleCop+, and if you like it, it will distinguish them ASAP.

Best regards,
Oleg Shuruev

Jul 2, 2010 at 11:38 AM


Thank you for your help. I already read about StyleCop+. It looks great and will help me. But about events I decided to redesign my pattern to use shorter and newest version of desclaration like public event EventHandler<Args> MyEvent. I found it ever the top :). Thanks again.