help with array of functions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Guest's Avatar

    help with array of functions

    Hi All,

    How can I create an array of functionsand how would I call them

    many thanks


  • mdb

    #2
    Re: help with array of functions

    <CobraStrikes@a l.com> wrote in news:d60o80$o8f $1@newsg2.svr.p ol.co.uk:
    [color=blue]
    > How can I create an array of functionsand how would I call them[/color]

    Look into delegates, and the Invoke() function.

    -mdb

    Comment

    • Arthur M.

      #3
      RE: help with array of functions

      If you are attempting to use different functions withint he same array then:

      1) Declare delegate types
      2) declare array of delegate[] type - use: (Delegate[] abc = new
      Delegate[n];) not to be confused with (delegate[] abc = new Delegate[n];)
      3) i

      delegate void testdelegate(in t param);

      class tclass
      {
      public void testfunction(in t p)
      {
      }
      }

      Delegate[] abc = new Delegate[10];
      for (int op_counter = 0; op_counter < abc.Length; op_counter++)
      {
      if (abc[op_counter] == null) continue;
      // if you know the delegate type then just use a cast
      ((testdelegate) abc[op_counter])(0);
      // if you dont know the type, then ether query or call
      invoke and catch an error
      abc[op_counter].DynamicInvoke( new object[] { op_counter });
      // validate delegate is actually pointing to the method you
      think it should be poiting to
      // use reflection for that - which parameters you are
      comparing is up to you

      //(typeof(tclass) .GetMethod("tes tfunctionname") ;
      // abc[op_counter].Method <-- will give you method info just
      as above

      }


      be careful with exceptions. Also remember, that dynamic invoke is quite slow
      comparing to statically defined types. Remmeber that dynamicinvoke takes
      object[] as a paramter, therefore any integer values will get boxed/unboxed
      (read in - performance issues).

      On a bright note, if implemented properly it works very well. I have
      implemented expression parser this way...

      - Arthur




      "CobraStrikes@a l.com" wrote:
      [color=blue]
      > Hi All,
      >
      > How can I create an array of functionsand how would I call them
      >
      > many thanks
      >
      >
      >[/color]

      Comment

      Working...