Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Insert function into event at runtime

Insert function into event at runtime

Scheduled Pinned Locked Moved C#
tutorialquestion
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bonzaiholding
    wrote on last edited by
    #1

    Hi, If i have a lot of function there is an easy way to insert them by runtime and not by code? For example instead of this:

    public class A
    {

        MyEvent m\_Event;
        List<MyEvent> m\_EventList;
    
        public bool Func\_1(int num) { MessageBox.Show("1"); return num > 1 ? true : false; }
        public bool Func\_2(int num) { MessageBox.Show("2"); return num > 2 ? true : false; }
        public bool Func\_3(int num) { MessageBox.Show("3"); return num > 3 ? true : false; }
        public bool Func\_4(int num) { return num > 4 ? true : false; }
        public bool Func\_5(int num) { return num > 5 ? true : false; }
        public bool Func\_6(int num) { return num > 6 ? true : false; }
        public bool Func\_7(int num) { return num > 7 ? true : false; }
        public bool Func\_8(int num) { return num > 8 ? true : false; }
    
        public A()
        {
                                   
            this.m\_Event += new MyEvent(Func\_1);
            this.m\_Event += new MyEvent(Func\_2);
            this.m\_Event += new MyEvent(Func\_3);
            this.m\_Event += new MyEvent(Func\_4);
            this.m\_Event += new MyEvent(Func\_5);
            this.m\_Event += new MyEvent(Func\_6);
            this.m\_Event += new MyEvent(Func\_7);
            this.m\_Event += new MyEvent(Func\_8);          
        }
    }
    

    I want this code to look like this:

    public class A
    {

        MyEvent m\_Event;
        List&lt;MyEvent&gt; m\_EventList;
    
        public bool Func\_1(int num) { MessageBox.Show("1"); return num &gt; 1 ? true : false; }
        public bool Func\_2(int num) { MessageBox.Show("2"); return num &gt; 2 ? true : false; }
        public bool Func\_3(int num) { MessageBox.Show("3"); return num &gt; 3 ? true : false; }
        public bool Func\_4(int num) { return num &gt; 4 ? true : false; }
        public bool Func\_5(int num) { return num &gt; 5 ? true : false; }
        public bool Func\_6(int num) { return num &gt; 6 ? true : false; }
        public bool Func\_7(int num) { return num &gt; 7 ? true : false; }
        public bool Func\_8(int num) { return num &gt; 8 ? true : false; }
    
        public A()
        {
                                   
            for(int i=1;i<=8;i++)
             this.m\_Event += new MyEvent(Func\_ + i.ToString());
    
        }
    }
    

    is there some way to do something like this?

    0 1 Reply Last reply
    0
    • B bonzaiholding

      Hi, If i have a lot of function there is an easy way to insert them by runtime and not by code? For example instead of this:

      public class A
      {

          MyEvent m\_Event;
          List<MyEvent> m\_EventList;
      
          public bool Func\_1(int num) { MessageBox.Show("1"); return num > 1 ? true : false; }
          public bool Func\_2(int num) { MessageBox.Show("2"); return num > 2 ? true : false; }
          public bool Func\_3(int num) { MessageBox.Show("3"); return num > 3 ? true : false; }
          public bool Func\_4(int num) { return num > 4 ? true : false; }
          public bool Func\_5(int num) { return num > 5 ? true : false; }
          public bool Func\_6(int num) { return num > 6 ? true : false; }
          public bool Func\_7(int num) { return num > 7 ? true : false; }
          public bool Func\_8(int num) { return num > 8 ? true : false; }
      
          public A()
          {
                                     
              this.m\_Event += new MyEvent(Func\_1);
              this.m\_Event += new MyEvent(Func\_2);
              this.m\_Event += new MyEvent(Func\_3);
              this.m\_Event += new MyEvent(Func\_4);
              this.m\_Event += new MyEvent(Func\_5);
              this.m\_Event += new MyEvent(Func\_6);
              this.m\_Event += new MyEvent(Func\_7);
              this.m\_Event += new MyEvent(Func\_8);          
          }
      }
      

      I want this code to look like this:

      public class A
      {

          MyEvent m\_Event;
          List&lt;MyEvent&gt; m\_EventList;
      
          public bool Func\_1(int num) { MessageBox.Show("1"); return num &gt; 1 ? true : false; }
          public bool Func\_2(int num) { MessageBox.Show("2"); return num &gt; 2 ? true : false; }
          public bool Func\_3(int num) { MessageBox.Show("3"); return num &gt; 3 ? true : false; }
          public bool Func\_4(int num) { return num &gt; 4 ? true : false; }
          public bool Func\_5(int num) { return num &gt; 5 ? true : false; }
          public bool Func\_6(int num) { return num &gt; 6 ? true : false; }
          public bool Func\_7(int num) { return num &gt; 7 ? true : false; }
          public bool Func\_8(int num) { return num &gt; 8 ? true : false; }
      
          public A()
          {
                                     
              for(int i=1;i<=8;i++)
               this.m\_Event += new MyEvent(Func\_ + i.ToString());
      
          }
      }
      

      is there some way to do something like this?

      0 Offline
      0 Offline
      0x3c0
      wrote on last edited by
      #2

      Yes. You can use System.Reflection and Delegate.CreateDelegate to help. The basic sequence of events is:

      1. Get the class' Type

      2. Get the EventInfo representing m_Event of that Type

      3. Iterate from one to eight

      4. Get the method "Func_" + i.ToString()

      5. Use Delegate.CreateDelegate(typeof(MyEvent), [MethodInfo you just retrieved in the last step])

      6. Store the return value of that function call in a variable

      7. Call the AddEventHandler(this, [Delegate you just created and stored in a variable])

      That's the basic sequence of events. If you wanted something simpler, but less modular, you could just create a List<MyEvent> and store the functions in there. Then just iterate and add the handler like you normally do.

      Between the motion And the act Falls the Shadow

      B 1 Reply Last reply
      0
      • 0 0x3c0

        Yes. You can use System.Reflection and Delegate.CreateDelegate to help. The basic sequence of events is:

        1. Get the class' Type

        2. Get the EventInfo representing m_Event of that Type

        3. Iterate from one to eight

        4. Get the method "Func_" + i.ToString()

        5. Use Delegate.CreateDelegate(typeof(MyEvent), [MethodInfo you just retrieved in the last step])

        6. Store the return value of that function call in a variable

        7. Call the AddEventHandler(this, [Delegate you just created and stored in a variable])

        That's the basic sequence of events. If you wanted something simpler, but less modular, you could just create a List<MyEvent> and store the functions in there. Then just iterate and add the handler like you normally do.

        Between the motion And the act Falls the Shadow

        B Offline
        B Offline
        bonzaiholding
        wrote on last edited by
        #3

        Thanks for your help , it's works :-\

        M 1 Reply Last reply
        0
        • B bonzaiholding

          Thanks for your help , it's works :-\

          M Offline
          M Offline
          mustang86
          wrote on last edited by
          #4

          Cool. I'm glad that you found a solution! I would have used a (yuck) macro. But, from what I could find, C# doesn't support macros. huh? However, I did find the following: A Macro Preprocessor in C# _____________ Joe

          P 1 Reply Last reply
          0
          • M mustang86

            Cool. I'm glad that you found a solution! I would have used a (yuck) macro. But, from what I could find, C# doesn't support macros. huh? However, I did find the following: A Macro Preprocessor in C# _____________ Joe

            P Offline
            P Offline
            PIEBALDconsult
            wrote on last edited by
            #5

            I just use the C-preprocesor. :rolleyes:

            M 1 Reply Last reply
            0
            • P PIEBALDconsult

              I just use the C-preprocesor. :rolleyes:

              M Offline
              M Offline
              mustang86
              wrote on last edited by
              #6

              I agree, cpp rules! :) http://gcc.gnu.org/onlinedocs/cpp/[^] http://gcc.gnu.org/onlinedocs/gcc-2.95.3/cpp_1.html#SEC1[^] In grad school, the assembler that I did (written in C) could do macros. How? By first running the assembler source code through cpp. :cool: Plus, the manual for cpp was already written. Double win! :) Heck, the smart engineer doesn't reinvent the wheel when it's just sitting there next to him/her! :) ___ Joe

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups