title: Declare callback event handlers correctly
body: <p>I have a simple delegate, event and property allowing me to create callback subscriptions on events:</p>
  
  <pre><code>public static class Test
  {
      /// &lt;summary&gt;Delegate for property changed event&lt;/summary&gt;
      public delegate void TestEventHandler();
  
      /// &lt;summary&gt;Event called when value is changed&lt;/summary&gt;
      public static event TestEventHandler OnTestHappening;
  
      /// &lt;summary&gt;Property to specify our test is happening&lt;/summary&gt;
      private static bool testHappening;
      public static bool TestHappening
      {
          get
          {
              return testHappening;
          }
          set
          {
              testHappening = value;
  
              // Notify our value has changed only if True
              // ie. Only fire an event when we're ready as we'll hook methods to the Event that must only fire if ready
              if ( value )
              {
                  if ( OnTestHappening != null )
                      OnTestHappening();
              }
          }
      }
  }
  </code></pre>
  
  <p>I can then easily subscribe and unsubscribe to the event and fire the event callbacks as needed:</p>
  
  <pre><code>public class Tester
  {
      private void Main()
      {
          Testing();
  
          // Start the test
          Test.TestHappening = true;
      }
  
      private void Testing()
      {
          // Unsubscribe from event
          Test.OnTestHappening -= Testing;
  
          // Check if we're busy testing yet
          if ( !Test.TestHappening )
          {
              // Subscribe to event
              Test.OnTestHappening += new Test.TestEventHandler( Testing );
  
              return;
          }
  
          // Do stuff here....
      }
  }
  </code></pre>
  
  <p>When compiling, code analysis gives me, "CA1009: Declare event handlers correctly?" and I've searched high and low and found many questions, articles etc but none that feel like they address my scenario.  I can't seem to find a concrete starting point for the conversion and I'm starting to wonder if I'm meant to completely rewrite the implementation?</p>
  
  <p>Edit: Firstly I really appreciate the assists, I did look carefully through all the sites I could before posting this and I did see (and try work with) each of the links that you all posted.  I even went back and studied delegates and events again but I feel like I'm missing the starting point somehow because each time I try change a part of it, I just keep producing errors that I can't come back from like:</p>
  
  <pre><code>public delegate void TestEventHandler( object sender, EventArgs e );
  </code></pre>
  
  <p>With the other links I visited, I could only find 1 similarity to my code (either in the delegate, the handler or the property) but couldn't find anything that related enough to mine to actually instil that "eureka" moment</p>
  
  <p>Edit 2: I have now rebuilt my example with what "looks" to be the correct standard but this code is so fugly it looks like it was beaten with a confogulus stick and dipped in a tank of confutious before being deep fried in horriduculous:</p>
  
  <pre><code>public static class Test
  {
      /// &lt;summary&gt;Delegate for property changed event&lt;/summary&gt;
      public delegate void TestEventHandler( object sender, EventArgs e );
  
      /// &lt;summary&gt;Event called when value is changed&lt;/summary&gt;
      public static event TestEventHandler OnTestHappening;
  
      /// &lt;summary&gt;Property to specify our test is happening&lt;/summary&gt;
      private static bool testHappening;
      public static bool TestHappening
      {
          get
          {
              return testHappening;
          }
          set
          {
              testHappening = value;
  
              // Notify our value has changed only if True
              // ie. Only fire an event when we're ready as we'll hook methods to the Event that must only fire if ready
              if ( value )
              {
                  if ( OnTestHappening != null )
                      OnTestHappening( null, EventArgs.Empty );
              }
          }
      }
  }
  
  public class Tester
  {
      private void Main()
      {
          Testing( this, EventArgs.Empty );
  
          // Start the test
          Test.TestHappening = true;
      }
  
      private void Testing( object sender, EventArgs e )
      {
          // Unsubscribe from the event
          Test.OnTestHappening -= Testing;
  
          // Check if we're busy testing yet
          if ( !GlobalClass.SystemOnline )
          {
              // Subscribe to the event
              Test.OnTestHappening += new Test.TestEventHandler( Testing );
  
              return;
          }
  
          // Do stuff here....
      }
  }
  </code></pre>
  
  <p>Please tell me I've missed something and that there is in fact a more elegant implementation</p>
  
  <p>Edit 3 : Based on the code by Enigmativity, I've reworked the code to it's most basic form. I've also moved the code setting the variable to true in a different method so it doesn't look so daft sitting in <code>Main</code>.</p>
  
  <pre><code>public static class Test4
  {
      /// &lt;summary&gt;Event called when value is changed&lt;/summary&gt;
      public static event EventHandler TestHappening;
  
      /// &lt;summary&gt;Property to specify our test is happening&lt;/summary&gt;
      private static bool test = false;
      public static bool Test
      {
          get
          {
              return test;
          }
          set
          {
              // Notify our value has changed only if True
              // ie. Only fire an event when we're ready as we'll hook methods to the Event that must only fire if ready
              if ( value )
              {
                  TestHappening( null, EventArgs.Empty );
              }
          }
      }
  }
  
  public class Tester4
  {
      private void Main()
      {
          Testing( this, EventArgs.Empty );
      }
  
      private void Testing( object sender, EventArgs e )
      {
          // Unsubscribe from the event
          Test4.TestHappening -= Testing;
  
          // Check if we're busy testing yet
          if ( !Test4.Test )
          {
              // Subscribe to the event
              Test4.TestHappening += Testing;
  
              return;
          }
  
          // Do stuff here....
      }
  
      private void SomeMethodCalledFromSomewhere()
      {
          // Set the value to true and thereby start the test
          Test4.Test = true;
      }
  }
  </code></pre>
  
  <ol>
  <li>Would this be considered good code or should I rather have the <code>OnTestHappening</code> method as defined in Enigmativity's code?</li>
  <li>Why can't I use a parameterless delegate?  It's now using the default <code>( object sender, EventArgs e )</code> but that feels overkill and doesn't make sense why the compiler is happy with it but according to coding standards it's considered bad code?  I'm not arguing the standard but rather trying to understand it's reasoning.</li>
  </ol>