Reputation: 61
I have 2 buttons doing the same function but in different condition. there code is something like this.
private void button1_Click(){
// do function1
if(condition){
...decrement an integer
//do function2
}
}
private void button2_Click(){
// do function1
if(another condition){
...increment an integer
//do function2
}
}
Can I pass condition1 and condition2 and increment, decrement to a method ?
Upvotes: 6
Views: 15526
Reputation: 1065
Yes you can. But you are still limited in what you can do. Consider this
public void Foo(Action action, Func<Boolean> someCondition) {
if (someCondition() == true) action();
}
Upvotes: 5
Reputation: 236248
Why not to extract duplicated code to methods?
private void Function1()
{
// do function1
}
private void Function2()
{
// do function2
}
private void button1_Click()
{
Function1() ;
if(condition)
{
//...decrement an integer
Function2();
}
}
private void button2_Click()
{
Function1();
if(another condition)
{
//...increment an integer
Function2();
}
}
If you have many similar methods with same structure, then consider creating
private void DoSomething(Func<bool> condition, Action action)
{
Function1();
if (condition())
{
action();
Function2();
}
}
And invoke it this way:
private int value;
private void button2_Click()
{
DoSomething(() => value < 5, () => value++);
}
Of course, if you need to pass some parameters to condition or action you should change Func
or Action
type. Also use Func
instead of Action
if you need to return some value from action.
private int value;
private void button2_Click()
{
DoSomething((x) => x % 2 == 0, (x) => x.ToString());
}
private void DoSomething(Func<int, bool> condition, Func<int, string> action)
{
Function1();
if (condition(value))
{
string result = action(value);
Function2();
}
}
If your condition and action is not that simple, use named methods instead of lambdas:
private bool FooCondition(int x)
{
// complex condition here
}
private string BarAction(int x)
{
// complex action here
}
private void button2_Click()
{
DoSomething(FooCondition, BarAction);
}
Upvotes: 1
Reputation: 2733
If you are using .NET 3.0, you could pass a Func to the method. Both classes which take parameters and no parameters exists.
A Func
returning a boolean and a parameter, could be written with a lambda like this:
condition => {
if (condition)
index++;
else
index--;
}
The Func
can be saved to a variable and passed around as wanted.
Upvotes: 0
Reputation: 1775
You can try something like this:
private void buttonClick()
{
DoSomething(condition ? true : false);
}
private void DoSomething(bool increment)
{
// do stuff
if (increment)
++index;
else
--index;
}
Upvotes: 1
Reputation: 710
You could potentially do this by wrapping condition 1 and condition 2 in methods and using them as a delegate, which allows them to be passed into a method.
For example, you could have the message with delegate signature expecting a method that takes x number of inputs and returns a boolean. Then you create methods matching that signature for both condition 1 and 2, and pass the appropriate one in to the method.
Inside the method, you simply call the delegate as if it were a method (which, in actuality, it is).
Check Microsoft's documentation surrounding delegates: http://msdn.microsoft.com/en-us/library/ms173171(v=vs.80).aspx
Upvotes: 0