6

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 ?

user1340443
  • 61
  • 1
  • 1
  • 2
  • you could pass a lambda to your method and execute it or not – lnu Apr 18 '12 at 06:22
  • Yes you can... but can you elaborate the question? what kind of condition are you talking abt here? inc,dec can be passed, yes. – Milee Apr 18 '12 at 06:31
  • @Sharmi condition is greater than or less than. – user1340443 Apr 18 '12 at 06:40
  • @user1340443 May be you should take a look at a similar question : http://stackoverflow.com/q/3324023/1278315 .May be that helps but i have not come across such a situation... :) esp. this http://stackoverflow.com/a/3324052/1278315 might help:) – Milee Apr 18 '12 at 06:49

5 Answers5

5

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();
}
whoblitz
  • 1,065
  • 1
  • 11
  • 17
  • 8
    Why write `if (condition() == true)` instead of simply `if (condition())`? – Adam Mihalcin Apr 18 '12 at 06:29
  • 3
    Personal preference, I consider it more readable. I also use condition() == false. – whoblitz May 07 '12 at 02:11
  • once the condition method is renamed, using for example : `if(IsAvailable() == true) { // do something}` will add more *noise* than simplicity of reading. Compare with `if(IsAvailable()) { // do something}` – hdoghmen Jun 30 '14 at 08:58
1

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;
    }
nickm
  • 1,775
  • 1
  • 12
  • 14
1

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);
}
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
0

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

Frater
  • 690
  • 3
  • 12
  • 1
    If these conditions are relatively simple, a [lambda](http://msdn.microsoft.com/en-us/library/bb397687.aspx) would probably be more readable that a separate method for each condition. – Adam Mihalcin Apr 18 '12 at 06:20
0

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.

Xharze
  • 2,703
  • 2
  • 17
  • 30