471,349 Members | 1,266 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,349 software developers and data experts.

Dynamic assign delegate function?

Hi

I have an odd question: is it possible to have a delegate that recieves
a methodname dynamically?

Like this: aClassObj.OnGo += new ANamespace.Invoke(fName);
where fName would be a string with the function name;

Maybe this is totally conceptually wrong, but right now I'm nont sure
which way to go.

I have a timer that trigger an event OnGo that fires a function within
a class and now I'd like to have a number of functions that can be
called and is decided in an xml document. Possibly a stupid idea, but i
don't know how to proceed.

TopObj->holds instance of callerObj
callerObj-> creates a instance of delegateObj
callerObj -> assigns one of its own functions to the event
delegateObj.OnGo

If it doesn't make any sense tell me to bury it and rewrite...:-(

Example on what I would like to do:
namespace ANamespace{
public delegate void Invoke(string ObjId);
Class A{
public event Invoke OnGo;
.....
}

Class B {

A aClassObj;
...
public B()
{
aClassObj = new A();
}
public void functionAssign(string fName)
{
aClassObj.OnGo += new ANamespace.Invoke(fName);
}

public void callerFunction(string ID){...}
}

}

Jul 21 '05 #1
3 2098
>I have an odd question: is it possible to have a delegate that recieves
a methodname dynamically?

Like this: aClassObj.OnGo += new ANamespace.Invoke(fName);
where fName would be a string with the function name;


Yes, with System.Delegate.CreateDelegate().

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Jul 21 '05 #2
Hi

Thanks for answering...but i have a question here:

Do you mean that you set a delgate on a delegate?

example:
public void callerFunction1(string ID){...}
public void callerFunction2(string ID){...}
public void callerFunction3(string ID){...}

program calls ->
functionAssign(callerFunction2) ;

/erik

Jul 21 '05 #3
Hi again Mattias,

Ignore my last...i just read up again on my delegates and Iunderstand
what you mean now. It works just perfectly...

I'm a bit slow :-)

/Erik

Jul 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Nate | last post: by
3 posts views Thread by lallous | last post: by
1 post views Thread by Nate | last post: by
3 posts views Thread by ogled | last post: by
4 posts views Thread by Dave | last post: by
2 posts views Thread by Luis Arvayo | last post: by
reply views Thread by pjr | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.