Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing a condition as a parameter

Is it possible to pass a condition as parameter as you do with actions?

Here's an example.

public void Test(Action action, Condition condition);

...

Test( () => Environment.Exit(0), () => variable == variable2 );
like image 290
Vittorio Romeo Avatar asked Aug 23 '11 19:08

Vittorio Romeo


People also ask

Can I pass a value as a parameter?

You can pass values to the parameters that are declared in the AccessProfile widget by reference, by value, or by specifying the direct value. Define this option in the main AccessProfile. This topic provides an example of an AccessProfile widget and a main AccessProfile.

What is passing a parameter?

Parameter passing involves passing input parameters into a module (a function in C and a function and procedure in Pascal) and receiving output parameters back from the module. For example a quadratic equation module requires three parameters to be passed to it, these would be a, b and c.


1 Answers

Try passing the second argument as type Func<Boolean>. The code should work as you have it in the second part of your question:

public void Text(Action action, Func<Boolean> condition) {
    if (condition()) action();
}

EDIT: Note that what you would be doing in your usage example is creating a Closure containing the captured variables variable and variable2. You should understand the implications of closures before using them in this way.

like image 76
Chris Shain Avatar answered Oct 06 '22 14:10

Chris Shain