无法访问矩阵内的属性

时间:2019-07-15 13:29:15

标签: c# matrix

基本上,我创建了一个类,并从该类中创建了一个矩阵,并尝试为该类的属性指定特定名称。

我已经尝试过对其进行初始化,但仍然无法正常工作

    public class Tile
    {
        public string TileName { get; set; }
        public int Status { get; set; }
    }
public class ChessBoard
    {
        private const int BOARDHEIGHT = 8;
        private const int BOARDWIDTH = 9;

        public Tile[,] InitializeBoard()
        {
            Tile[,] newBord = new Tile[BOARDHEIGHT, BOARDWIDTH];

            for (int x = 0; x < BOARDWIDTH; x++)
            {
                for (int y = 0; y < BOARDHEIGHT; y++)
                {
                    newBord[x, y].TileName = x+"-"+y;
                    newBord[x, y].Status = 0;
                }
            }
            return newBord;
        }

        public void DisplayBoard(Tile[,] Board)
        {
            for (int x = 0; x < BOARDWIDTH; x++)
            {
                for (int y = 0; y < BOARDHEIGHT; y++)
                {
                    Console.WriteLine(Board[x,y].TileName);
                }
                Console.WriteLine();
            }
        }

    }

        static void Main(string[] args)
        {
            ChessBoard CB = new ChessBoard();
            Tile[,] CBoard = CB.InitializeBoard();
            CB.DisplayBoard(CBoard);
        }

我想尝试使用其索引命名Tile,但是只有“ System.NullReferenceException:对象引用未设置为对象的实例。”

0 个答案:

没有答案