Jim, I'm trying to learn delegates in C# myself.
One problem with your code is that you have not assigned the delegate to a valid handler. The signature of the delegate has to be matched to a valid handler with the same method signature.
In the line:
this.MyFuncToCallFrmApp = new MyFunctionDelegate(this.MyFuncToCallFrmApp);
this.MyFuncToCallFrmApp
is a delegate, but it needs to be a valid method handler instead.
Here is how you pass in a method handler:
public delegate int MyFunctionDelegate(int _some, string _args);
MyFunctionDelegate MyFuncToCallFrmApp = new MyFunctionDelegate(PrintValues);
// the method I'm mapping has a valid signature for the delegate I'm mapping to:
public void PrintValues(int some, string args)
{
Console.WriteLine(string.Format("Some = {0} & Args = {1}", some.ToString(), args));
}
Hopefully the link below and sample code will be of some help to you:
Delegates Tutorial
A delegate in C# is similar to a function pointer in C or C++. Using a delegate allows the programmer to encapsulate a reference to a method inside a delegate object. The delegate object can then be passed to code which can call the referenced method, without having to know at compile time which method will be invoked. Unlike function pointers in C or C++, delegates are object-oriented, type-safe, and secure. (MSDN)
public class MyClass
{
// a delegate by definition is a collection of pointers to method handlers
// I declare my delegate on this line
// PLEASE NOTE THE SIGNATURE!
public delegate void MyFunctionDelegate(int some, string args);
public MyClass() : base()
{
// instantiate the delegate (AKA create the pointer)
MyFunctionDelegate myFunctionDelegate = new MyFunctionDelegate();
// map a valid method handler (WITH THE SAME SIGNATURE) to this delegate
// I'm using "+=" operator because you can add more than one handler to a collection
myFunctionDelegate += new MyFunctionDelegate(PrintValues);
// invoke the method handler (which in this case is PrintValues() - see below)
// NOTE THE SIGNATURE OF THIS CALL
myFunctionDelegate(1, "Test");
}
// this is the handler method that I am going to map to the delegate
// AGAIN, PLEASE NOTE THE SIGNATURE
public void PrintValues(int some, string args)
{
Console.WriteLine(string.Format("Some = {0} & Args = {1}", some.ToString(), args));
}
}