Link to code

I am wondering if this can be done in C #.

Let's say I have a class with these methods:

public class Ladder
{
    private int currentStep = 0;

    Ladder Up()
    {
        currentStep++;
        return this;
    }

    Ladder Down()
    {
        currentStep--;
        return this;
    }
}

      

I can use it like this:

Ladder ladder = new Ladder();

ladder.Up().Up().Up().Down().Down();

      

Now I would like to add a conditional method that can be used like this:

ladder.IF(somecondition, Up(), Down());

      

Meaning if somecondition == true then do Up (), else Down ()

Can this be done? I've been thinking about using anonymous methods, but can't figure out how to reference the "this" instance so that it knows what these functions refer to.

Any help is greatly appreciated!

+3


source to share


6 answers


This will probably be close to what you're asking, although I wouldn't say it would make the code more understandable for future maintainers:

Ladder If(bool condition,
    Func<Ladder, Ladder> @true, 
    Func<Ladder, Ladder> @false)
{
    return condition ? @true(this) : @false(this);
}

      



Using:

ladder
    .Up()
    .Down()
    .If(true, l => l.Up(), l => l.Down())
    .Up()
    .Down();

      

+5


source


You don't need a conditional method. Use the? Operator:



somecondition ? ladder.Up() : ladder.Down();

      

+3


source


You can add another method Go

that encapsulates your requirement, you have 2 options - perhaps implement both:

public Ladder Go(bool condition)
{
    if(condition)
      Up();
    else
      Down();
    return this;
}

public Ladder Go(Func<bool> condition)
{
    return this.Go(condition());
}

      

This allows you to specify a boolean, inline condition or pass a function

ladder.Go(true);
ladder.Go(someValue > 0);
ladder.Go( someMethodWhichTakesNoParamsAndReturnsBool )

      

+2


source


This is the way to write this method:

public Ladder If(bool someCondition, Func<Ladder> trueFunc, Func<Ladder> falseFunc)
    {
        return someCondition ? trueFunc() : falseFunc();
    }

      

And the way to call it

ladder.If(condition, ladder.Up, ladder.Down);

      

+2


source


This is the closest access to what you want - there is an extension method called If

that takes actions to execute on the stairs. These actions are defined as static methods in your Ladder class. Then you use the static import function to import your Ladder class.

// This import is really important - it lets you refer to Up, 
// rather than Ladder.Up.
using static Ladder;

void Main()
{
    Ladder ladder = new Ladder();

    // Basic usage
    ladder.If(true, Up, Down);
    Console.WriteLine(ladder.ToString());

    // And demonstrating chaining.
    ladder.If(false, Up, Down).If(false, Up, Down);
    Console.WriteLine(ladder.ToString());
}

public class Ladder
{
    private int currentStep = 0;

    public Ladder Up()
    {
        currentStep++;
        return this;
    }

    public Ladder Down()
    {
        currentStep--;
        return this;
    }

    // Static methods for Up and Down a ladder.
    public static void Up(Ladder ladder)
    {
        ladder.Up();
    }

    public static void Down(Ladder ladder)
    {
        ladder.Down();
    }

    public override string ToString()
    {
        return "I'm on step " + currentStep;
    }
}


public static class Extensions
{
    // Extension method to conditionally take an action on the ladder.
    public static Ladder If(this Ladder ladder, bool condition, Action<Ladder> trueAction, Action<Ladder> falseAction)
    {
        if (condition)
        {
            trueAction(ladder);
        }
        else
        {
            falseAction(ladder);
        }

        return ladder;
    }
}

      

0


source


You can add another method in the class Ladder (using: using System.Reflection;

)

public Ladder IF(bool condition, string trueFunc, string falseFunc)
{
    MethodInfo trueMethod = this.GetType().GetMethod(trueFunc);
    MethodInfo falseMethod = this.GetType().GetMethod(falseFunc);
    return condition ? (Ladder)trueMethod.Invoke(this, null) : (Ladder)falseMethod.Invoke(this, null);
}

      

And use it like this:

ladder.IF(true, "Up", "Down"); // goes up
ladder.IF(false, "Up", "Down"); // goes down
ladder.IF(true, "Down", "Up"); // goes down
ladder.IF(false, "Down", "Up"); // goes up

      

0


source







All Articles