Delegate Functions with Arguments
I am trying to get Delegate functions to work with parameters
I added "String str" to the Delegate declaration at the top and then to both
the functions f1 and f12 - . Taking these out and having no arguments
allows the code to compile and run ok - Trouble is putting these in and the
code gets hung up on the Invoke statement - I tried changing the Invoke statement in various ways but can not come up with anything to make it work.
Is there a way to get functions with arguments to work in this situation?
Here is the code that I am playing with:
import
import System.Collections.ArrayList;
/** @delegate */
public
delegatevoid MyDelegate( String str );// delegate declarationpublic
class MyClass{
private ArrayList list =new ArrayList(10);
privateint no = 0;
/** @event */
publicvoid add_MyEvent(MyDelegate listener)
{
list.Add(listener);
}
/** @event */
publicvoid remove_MyEvent(MyDelegate listener)
{
list.Remove(listener);
}
publicvoid FireAway()
{
System.Object [] toArray = list.ToArray();
int len = toArray.length;
for (int i = 0;i < len ;i++)
{
((MyDelegate)(toArray
}
}
}
publicclass MainClass
{
staticpublicvoid main (String [] args)
{
new MainClass();
}
publicvoid f1( String str )
{
Console.WriteLine("This is called when the event fires. ");
}
publicvoid f12( String str )
{
Console.WriteLine("This Next ");
}
public MainClass()
{
MyClass i =new MyClass();
i.add_MyEvent(new MyDelegate(this.f1));
i.add_MyEvent(new MyDelegate(this.f12));
i.FireAway();
}
}
- thanks

