How to return 2 or 3 values ​​using Tuple?

So, I am doing a stupid game in C #, but I'm a little stuck here.

I try to return either 2 values ​​or 3 values, depending on the rules of the game.

I need to know how I can return different amounts of values ​​in only one function. As sometimes I need to return 3 values, and sometimes 7 values ​​... in one function.

An error with a variable tuple, of course, since I cannot return less than 3 values.

public Tuple<int, int, int> movepion(schaken.pion pion, int row, int column)
{
    int row2 = row + 1;

    //posities berekenen
    if (pion.kleur == Color.Blue)
    {
        if (pion.volgnr == 0) { row++; row2++; }
        else { row++; }
    }
    else
    {
    }

    // declaration
    var tuple = new Tuple<int, int>(row, column);
    var tuple2 = new Tuple<int, int, int>(row, row2, column);

    // what position?
    if (pion.volgnr == 0)
    {
        pion.volgnr = pion.volgnr + 1;
        return tuple2;
    }
    else
    {
        return tuple;
    }
}
+4
source share
2 answers

Return the collection instead Tuple<>

public IEnumerable<int> movepion(schaken.pion pion, int row, int column)
{
    IEnumerable<int> result;
    bool return2Items = false; //Some logic part to decide how many items to return
    if(return2Items)
        result = new List<int> { 1, 2 };
    else
        result = new List<int> { 1, 2, 3 };

   return result;    
}

After a better understanding of the comments, which I recommend creating an object MoveOption, and that the function will returnIEnumerable<MoveOption>

public class MoveOption
{
    public int X { get; set; }
    public int Y { get; set; }
}

Then in your function:

public IEnumerable<MoveOption> movepion(schaken.pion pion, int row, int column)
{
    List<MoveOption> options = new List<MoveOption>();

    if(/*some condition*/)
    {
        options.Add(new MoveOption{ X = 1, Y = 2 });
    }

    if(/*some condition*/)
    {
        options.Add(new MoveOption{ X = 5, Y = 7 });
    }

    //Rest of options

    return options;
}

, , :

public interface IMoveOptionCalcumator
{
    public MoveOption Calculate(/*input*/);
}

public class MoveOptionX : IMoveOptionCalcumator
{
    public MoveOption Calculate(/*input*/) { /*some implementation*/ }
}

:

public class YourClass
{
    public IEnumerable<IMoveOptionCalcumator> MoveOptionCalculators { get; set; }

    public YourClass()
    {
        // Initialize the collection of calculators 
        // You can look into Dependency Injection if you want even another step forward :)
    }


    public IEnumerable<int> movepion(schaken.pion pion, int row, int column)
    {
        List<MoveOption> options = new List<MoveOption>();
        foreach (var item in MoveOptionsCalculators)
        {
            var result = item.Calculate(/*input*/);
            if(result != null)
            {
                options.Add(result);
            }
        }
        return options;
    }
}
+10

Tuple<int, int, int?>

var tuple = new Tuple<int, int, int?>(row, column, null);

, , , , .

+2

Source: https://habr.com/ru/post/1651789/


All Articles