Delegate Functions with Arguments

Hi,

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 System.*;
import System.Collections.ArrayList;

/** @delegate */

publicdelegatevoid MyDelegate( String str );// delegate declaration

publicclass 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);
}

public
void FireAway()
{
System.Object [] toArray = list.ToArray();
int len = toArray.length;

for
(int i = 0;i < len ;i++)
{
((MyDelegate)(toArrayIdea)).Invoke();
}
}
}

public
class MainClass
{
staticpublicvoid main (String [] args)
{
new MainClass();
}

public
void f1( String str )
{
Console.WriteLine("This is called when the event fires. ");
}

public
void 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
[4225 byte] By [barbbayne] at [2008-2-14]
# 1
I am so stupid

all I had to do was put the arguments inside the Invoke brackets
so so sorry to be cluttering up things

the fix is simply:

((MyDelegate)(toArray\[i\])).Invoke( "a string" );

So Sorry to waste your time

barbbayne at 2007-9-9 > top of Msdn Tech,Visual J#,Visual J# General...