Fixing CS1525 error with bitboards and returning ulongs

Peggun 20 Reputation points
2024-07-28T09:15:53.79+00:00

I am building a chess engine and need to use bitboards. I wrote a function that gets the Bitboards, but now I am getting a CS1525 compiler error with the ref statements. Can anyone suggest a workaround? I am still learning about C#. Thank you for any help you can provide.

Here is the function

private ref ulong GetBitboard(Piece.PieceType pieceType)
{
    return ref pieceType switch
    {
        Piece.PieceType.WhitePawn => ref whitePawns,
        Piece.PieceType.WhiteKnight => ref whiteKnights,
        Piece.PieceType.WhiteBishop => ref whiteBishops,
        Piece.PieceType.WhiteRook => ref whiteRooks,
        Piece.PieceType.WhiteQueen => ref whiteQueens,
        Piece.PieceType.WhiteKing => ref whiteKing,
        Piece.PieceType.BlackPawn => ref blackPawns,
        Piece.PieceType.BlackKnight => ref blackKnights,
        Piece.PieceType.BlackBishop => ref blackBishops,
        Piece.PieceType.BlackRook => ref blackRooks,
        Piece.PieceType.BlackQueen => ref blackQueens,
        Piece.PieceType.BlackKing => ref blackKing,
        _ => throw new ArgumentException("Invalid Piece Type"),
    };
}
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,918 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 117.3K Reputation points
    2024-07-28T13:43:24.42+00:00

    If the switch expression does not work, then try the switch statement:

    private ref ulong GetBitboard(Piece.PieceType pieceType)
    {
        switch( pieceType )
        {
            case Piece.PieceType.WhitePawn: return ref whitePawns;
            case Piece.PieceType.WhiteKnight: return ref whiteKnights;
            ... and so no ...
            default: throw new ArgumentException("Invalid Piece Type");
        };
    }
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.