Kittoes0124
Kittoes0124

Reputation: 5080

How do I pass arguments to delegates?

I have the following pseudo code that works:

KeyBindings[Keys.Right] += Method1;
KeyBindings[Keys.Right] += Method2;

The problem I'm running into is that I would like to be able to do this:

KeyBindings[Keys.Right] += Method1(argument);
KeyBindings[Keys.Right] += Method2(argument1, argument 2);

Is this possible? If so, how do I rewrite my code to achieve this?


KeyBindings is defined as:

Dictionary<Keys, Action> KeyBindings = new Dictionary<Keys, Action>();

Upvotes: 0

Views: 197

Answers (4)

Dmitry Nogin
Dmitry Nogin

Reputation: 3750

class Program {
    public static void Main() {
        Dictionary<ConsoleKey, Action> KeyBindings = new Dictionary<ConsoleKey, Action>();
        KeyBindings[ConsoleKey.A] = null;
        KeyBindings[ConsoleKey.A] += () => Method1(12);
    }

    static void Method1(int arg) {
    }
}

Upvotes: 3

Piotr Auguscik
Piotr Auguscik

Reputation: 3681

To do such thing you have to create new method that has those args fixed inside. Or anonymus method like this:

event += (s, someEventArgs) => Method1(s, someEventArgs, someOtherArgs)

Upvotes: 0

Jeff Mercado
Jeff Mercado

Reputation: 134621

Well in general, if you wanted to create a delegate that would take some existing function and apply a preset set of arguments, you would need to wrap the function inside another.

e.g.,

Consider function Foo() that takes one string argument and returns an int (which has the same signature as Func<string, int>):

int Foo(string str)
{
    return str.Length + 8941;
}

If you wanted to use this to create a delegate that returns the result of calling Foo of the string "bar", you could do this:

Func<int> foobar = () => Foo("bar");

So notice that we created a new delegate, the lambda expression that takes nothing and (in this case) returns the result of calling Foo("bar").


You can apply the same thing in your code:

KeyBindings[Keys.Right] += new Action(() => Method1(argument));
KeyBindings[Keys.Right] += new Action(() => Method2(argument1, argument 2));

Upvotes: 3

Brandon Moore
Brandon Moore

Reputation: 8800

It doesn't work that way. You are not calling a method at this point... you are simply telling it which method it should use.

When you create an event, you specify what kind of method it takes which includes the parameters. The parameters are passed at the place where the event is raised.

Here is a tutorial on the subject that will help you understand better: http://www.csharp-station.com/Tutorials/lesson14.aspx

Looking at the code below may also help you put the pieces together a little better.

public void SomeMethod()
{
    //Assign the delegate
    KeyBindings[Keys.Right] += Method1;
}

public PlaceWhereEventGetsRaised()
{
    object argument1, argument2;

    // set the arguments to something

    if (KeyBindings[Keys.Right] != null) 
        KeyBindings[Keys.Right](argument1, argument2);
}

public void Method1(object argument1, object argument2)
{
    // Do stuff
}

If it looks like you are unable to accomplish what you were wanting to do the way you thought, then let us know what you're trying to do and maybe we can help you find the right approach.

Upvotes: 0

Related Questions