RexMan85
RexMan85

Reputation: 59

Delegates with specific arguments

I'll try to keep it as simple as I can. I have a delegate DEL which is a void() (no arguments). I have a function FUNC(int). I have two variables A and B. I want to be able to do A() and it will run FUNC(10), and be able to do B() and it will run FUNC(43). I know I can have the delegate be void(int) but I also have a function FUNC2(int, int). and a variable C that will run FUNC2(21,6). I need A, B and C to all be the same type of variable.

Upvotes: 0

Views: 59

Answers (1)

mlorbetske
mlorbetske

Reputation: 5649

It sounds like you have this

public delegate void DEL();

public class MyClass
{
    public static void FUNC(int value)
    {
    }

    public static void FUNC2(int value1, int value2)
    {
    }
}

And you need variables A, B and C to be declared and initialized to some delegate type such that they call FUNC(10), FUNC(43) and FUNC2(21, 6) respectively when invoked.

The code for A

DEL A = () => MyClass.FUNC(10);

The code for B

DEL B = () => MyClass.FUNC(43);

And, the code for C

DEL C = () => MyClass.FUNC2(21, 6);

A, B and C are all of type DEL

You could also express this as:

DEL A = new DEL(() => MyClass.FUNC(10));

or

DEL A = new DEL(CallFUNCWithValue10);

public static void CallFUNCWithValue10()
{
    MyClass.FUNC(10);
}

or just

DEL A = CallFUNCWithValue10;

Upvotes: 2

Related Questions