1

The code below throws the following exception:

Error 1 Cannot implicitly convert type 'Clarity.DevTest.Exercises.ChessPosition' to 'System.Collections.Generic.IEnumerable< Clarity.DevTest.Exercises.ChessPosition>'. An explicit conversion exists (are you missing a cast?)

Can someone explain to me whats going wrong and why it cant return the result?

using System;
using System.Collections.Generic;

namespace Clarity.DevTest.Exercises
{
public class Exercise1
{
    public IEnumerable<ChessPosition> GetLegalMoves(ChessPosition rookToMove, ChessPosition friendlyRook, ChessPosition enemyRook1, ChessPosition enemyRook2)
    {

        //create array to hold positions
        ChessPosition[,] coords = new ChessPosition [8,8];
        //set the Rooks into the correct places on the array
        //eg rookToMove.X = 4, rookToMove.Y = 5, "R" would be places in 4,5
        coords[rookToMove.X, rookToMove.Y] = "R";
        coords[friendlyRook.X, friendlyRook.Y] = "F";
        coords[enemyRook1.X, enemyRook1.Y] = "1";
        coords[enemyRook2.X, enemyRook2.Y] = "2";

        //throw new NotImplementedException();

        ChessPosition result = new ChessPosition(4, 5);
        return result;
    }
}

public partial class ChessPosition
{
    public ChessPosition(int x, int y)
    {
        //check to see if out of bounds
        if (x < 1 || x > 8 || y < 1 || y > 8)
        {
            throw new ArgumentException("x and y must be in the range 1-8");
        }
        X = x;
        Y = y;
    }

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

3 Answers 3

2

You're returning a single ChessPosition instead of an Enumerable collection of results.

Either return an enumerable collection:

ChessPosition result = new ChessPosition(4, 5);
IEnumerable<ChessPosition> results = new List<ChessPosition>();
results.Add(result);

return results;

or change your return type:

public ChessPosition GetLegalMoves(
    ChessPosition rookToMove, ChessPosition friendlyRook, ChessPosition 
    enemyRook1, ChessPosition enemyRook2)
Sign up to request clarification or add additional context in comments.

Comments

2

your method says it's returning a IEnumerable<ChessPosition> but you actually return a ChessPosition.

your method signature is:

public IEnumerable<ChessPosition> GetLegalMoves(...

So you are telling anyone who accesses that method they will receive a collection of ChessPosition.

But you return:

ChessPosition result = new ChessPosition(4, 5);
return result;

which is not a collection, but a single ChessPosition.

You need to decide whether your method returns either a single ChessPosition, or many.

Comments

1

The return type of your method 'GetLegalMoves' does not match what you are returning - change the return type to remove the ienumerable, so it's just returning 'ChessPosition'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.