Uploaded image for project: 'Rules Repository'
  1. Rules Repository
  2. RSPEC-3906

Event Handlers should have the correct signature

    XMLWordPrintable

    Details

    • Type: Code Smell Detection
    • Status: Active
    • Resolution: Unresolved
    • Labels:
    • Message:
      Change the signature of that event handler to match the specified signature.
    • Highlighting:
      Hide

      delegate declaration

      Show
      delegate declaration
    • Default Severity:
      Major
    • Impact:
      Low
    • Likelihood:
      High
    • Targeted languages:
      VB.Net
    • Covered Languages:
      C#
    • Irrelevant for Languages:
      ABAP, C, C++, Cobol, CSS, Flex, HTML, Java, JavaScript, Objective-C, PHP, PL/I, PL/SQL, Python, RPG, Swift, VB6, XML
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      5min
    • Analysis Scope:
      Main Sources, Test Sources
    • FxCop:
      DeclareEventHandlersCorrectly, CA1009

      Description

      Delegate event handlers (i.e. delegates used as type of an event) should have a very specific signature:

      • Return type void.
      • First argument of type System.Object and named 'sender'.
      • Second argument of type System.EventArgs (or any derived type) and is named 'e'.

      This rule raises an issue whenever a delegate declaration doesn't match that signature.

      Noncompliant Code Example

      public delegate void AlarmEventHandler(object s);
      
      public class Foo
      {
          public event AlarmEventHandler AlarmEvent; // Noncompliant
      }
      

      Compliant Solution

      public delegate void AlarmEventHandler(object sender, AlarmEventArgs e);
      
      public class Foo
      {
          public event AlarmEventHandler AlarmEvent; // Compliant
      }
      

      See

      Handling and Raising Events

        Attachments

          Activity

            People

            Assignee:
            Unassigned Unassigned
            Reporter:
            jeanchristophe.collet Jean-Christophe Collet (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Dates

              Created:
              Updated: