sign in | join about | help | sitemap
Connecting Tech Pros Worldwide
's Avatar

help with array of functions


Question posted by: (Guest) on November 17th, 2005 03:03 AM
Hi All,

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

many thanks


2 Answers Posted
mdb's Avatar
Guest - n/a Posts
#2: Re: help with array of functions

<CobraStrikes@al.com> wrote in news:d60o80$o8f$1@newsg2.svr.pol.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
Arthur M.'s Avatar
Guest - n/a Posts
#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(int param);

class tclass
{
public void testfunction(int 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("testfunctionname");
// 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@al.com" wrote:
[color=blue]
> Hi All,
>
> How can I create an array of functionsand how would I call them
>
> many thanks
>
>
>[/color]
 
Not the answer you were looking for? Post your question . . .
196,830 members ready to help you find a solution.
Join Bytes.com

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 196,830 network members.
Post your question now . . .
It's fast and it's free

Popular Articles

Top Community Contributors