c# - Declare callback event handlers correctly -


i have simple delegate, event , property allowing me create callback subscriptions on events:

public static class test {     /// <summary>delegate property changed event</summary>     public delegate void testeventhandler();      /// <summary>event called when value changed</summary>     public static event testeventhandler ontesthappening;      /// <summary>property specify our test happening</summary>     private static bool testhappening;     public static bool testhappening     {                 {             return testhappening;         }         set         {             testhappening = value;              // notify our value has changed if true             // ie. fire event when we're ready we'll hook methods event must fire if ready             if ( value )             {                 if ( ontesthappening != null )                     ontesthappening();             }         }     } } 

i can subscribe , unsubscribe event , fire event callbacks needed:

public class tester {     private void main()     {         testing();          // start test         test.testhappening = true;     }      private void testing()     {         // unsubscribe event         test.ontesthappening -= testing;          // check if we're busy testing yet         if ( !test.testhappening )         {             // subscribe event             test.ontesthappening += new test.testeventhandler( testing );              return;         }          // stuff here....     } } 

when compiling, code analysis gives me, "ca1009: declare event handlers correctly?" , i've searched high , low , found many questions, articles etc none feel address scenario. can't seem find concrete starting point conversion , i'm starting wonder if i'm meant rewrite implementation?

edit: firstly appreciate assists, did through sites before posting , did see (and try work with) each of links posted. went , studied delegates , events again feel i'm missing starting point somehow because each time try change part of it, keep producing errors can't come like:

public delegate void testeventhandler( object sender, eventargs e ); 

with other links visited, find 1 similarity code (either in delegate, handler or property) couldn't find related enough mine instil "eureka" moment

edit 2: have rebuilt example "looks" correct standard code fugly looks beaten confogulus stick , dipped in tank of confutious before being deep fried in horriduculous:

public static class test {     /// <summary>delegate property changed event</summary>     public delegate void testeventhandler( object sender, eventargs e );      /// <summary>event called when value changed</summary>     public static event testeventhandler ontesthappening;      /// <summary>property specify our test happening</summary>     private static bool testhappening;     public static bool testhappening     {                 {             return testhappening;         }         set         {             testhappening = value;              // notify our value has changed if true             // ie. fire event when we're ready we'll hook methods event must fire if ready             if ( value )             {                 if ( ontesthappening != null )                     ontesthappening( null, eventargs.empty );             }         }     } }  public class tester {     private void main()     {         testing( this, eventargs.empty );          // start test         test.testhappening = true;     }      private void testing( object sender, eventargs e )     {         // unsubscribe event         test.ontesthappening -= testing;          // check if we're busy testing yet         if ( !globalclass.systemonline )         {             // subscribe event             test.ontesthappening += new test.testeventhandler( testing );              return;         }          // stuff here....     } } 

please tell me i've missed , there in fact more elegant implementation

edit 3 : based on code enigmativity, i've reworked code it's basic form. i've moved code setting variable true in different method doesn't daft sitting in main.

public static class test4 {     /// <summary>event called when value changed</summary>     public static event eventhandler testhappening;      /// <summary>property specify our test happening</summary>     private static bool test = false;     public static bool test     {                 {             return test;         }         set         {             // notify our value has changed if true             // ie. fire event when we're ready we'll hook methods event must 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 event         test4.testhappening -= testing;          // check if we're busy testing yet         if ( !test4.test )         {             // subscribe event             test4.testhappening += testing;              return;         }          // stuff here....     }      private void somemethodcalledfromsomewhere()     {         // set value true , thereby start test         test4.test = true;     } } 
  1. would considered code or should rather have ontesthappening method defined in enigmativity's code?
  2. why can't use parameterless delegate? it's using default ( object sender, eventargs e ) feels overkill , doesn't make sense why compiler happy according coding standards it's considered bad code? i'm not arguing standard rather trying understand it's reasoning.

as per storm's request, here how structure code. it's more inline standard conventions.

public static class testclass {     public delegate void testeventhandler(object sender, eventargs e);      public static event testeventhandler testhappening;      private static bool test = false;     public static bool test     {                 {             return test;         }         set         {             test = value;             if (test)             {                 ontesthappening();             }         }     }      private static void ontesthappening()     {         var handler = testhappening;         if (handler != null)             handler(null, eventargs.empty);     } } 

and tester this:

public class tester {     public void main()     {         testclass.testhappening += testing;         go();     }      private void testing(object sender, eventargs e)     {         console.writeline(testclass.test);         testclass.testhappening -= testing;     }      private void go()     {         testclass.test = true;     } } 

calling this:

var tester = new tester(); tester.main(); 

running outputs true console.


if writing in more standard way, this:

public class testeventarg : eventargs {     public testeventarg(bool updatedvalue)     {         this.updatedvalue = updatedvalue;     }     public bool updatedvalue { get; private set; } }  public class testclass {     public event eventhandler<testeventarg> testhappening;      private bool test = false;     public bool test     {         { return test; }         set         {             var old = test;             test = value;             if (test != old)                 ontesthappening(test);         }     }      private void ontesthappening(bool updatedvalue)     {         var handler = testhappening;         if (handler != null)             handler(this, new testeventarg(updatedvalue));     } } 

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -