-1

I am trying to make a chessgame in c# WPF. I have miltiple classes, one for every kind of chesspiece and they all implement the same interface. All pieces objects are stored in a 2d array of type object[,] (of which have a feeling it's not the right way). I want to loop through this array to draw every corresponding image by calling Board[x, y].ImgURI but i get:

CS1061 'object' does not contain a definition for 'ImgURI' and no accessible extension method 'ImgURI' accepting a first argument of type 'object' could be found

 class Rook : Igamepiece{...}
 class King : Igamepiece{...}
 class Queen : Igamepiece{...}

 interface Igamepiece{
     public string ImgURI { get; set; }     //property that holds the image Uri
 }
 class Main{
     public object[,] Board = new object[8, 8];         //array containing objects of different types

     for (int y = 0; y < Board.GetLength(0); y++)
        {
            for (int x = 0; x < Board.GetLength(1); x++)
            {
                GameArea.Children.Add(new Image
                {
                    Source = new BitmapImage(new Uri(Board[x, y].ImgURI, UriKind.Relative)),
                    Width = SquareSize,
                    Height = SquareSize,
                    Margin = new Thickness(nextX, nextY, 0, 0)
                });
                nextX += SquareSize;
            }
                nextX = 0;
                nextY += SquareSize;
        }
 }           

 
1
  • 1
    When you create a collection of type object, there is a good chance you are doing something suspect. Commented Nov 13, 2020 at 2:54

1 Answer 1

1

Just change:

public object[,] Board = new object[8, 8]; 

to:

public Igamepiece[,] Board = new Igamepiece[8, 8]; 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.