Delegate Functions
I have been trying to understand Delegate Functions - I am able to store multiple
functions in one Delegate by using something like:
public
void add_MyEventHandler( MyEventHandler listener)
{
if (MyEventHandler !=null)
{
MyEventHandler = (MyEventHandler)System.Delegate.Combine(MyEventHandler, listener);
}
else
{
MyEventHandler = listener;
}
}
i.add_MyEventHandler(
new MyEventHandler(this.f1));
i.add_MyEventHandler(new MyEventHandler(this.f2));when MyEventHandler is invoked, both functions are triggered
ie: MyEventHandler.Invoke();
I was wondering if there is a way to only invoke a particular function rather than
all of them that may have been added with the Combine method, and is there a way to get at the name of the function?
thanks
The whole idea behind delegates and events is decoupling of classes, meaning that one class doesn't have to know what another class is doing.
That's why there is no way for one class to know who the registered event handlers are or the names of their event handling methods.
Here is a very good introduction to delegates: http://www.thecodeproject.com/csharp/delegate_bedtime.asp
thanks so much for the info - I have been trying to come up with something similar to what this article talks about:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwinforms/html/printwinforms.asp
trouble is it is wrtten in VB - so I wanted something in J# - I've got somehing working now by adding the functions into an array instead of using Combine and then I maintain a separtate "FunctonID" list that is maintained in sync with the
Funtion array, then I just pass the FunctionID that I want to invoke.
anyway I've got around that problem now
thanks
Our Instant J# VB to J# converter will translate this for you.
Download the Demo at www.tangiblesoftwaresolutions.com
David Anton
www.tangiblesoftwaresolutions.com
Home of:
Clear VB: Cleans up outdated VB.NET code
Instant C#: Converts from VB.NET to C#
Instant VB: Converts from C# to VB.NET
Instant J#: Converts from VB.NET to J#