将值赋给 For 循环内对象数组的 属性

Assigning value to property of array of object inside For loop

背景: 我正在为我的学校作业编写一个益智游戏。我有一个保存在文本文件中的益智游戏。现在我通过读取保存的文本文件将其加载到表单中。在文本文件中,前两行是图块(2d PictureBoxes)的总行数和列数,后面的行是每个 PictureBox 的行号、列号和图像索引。

方法: 我正在使用 Tile 的二维数组(继承自 PictureBox Class 的自定义控件 class)引用并使用嵌套循环来读取每个图块的 (PictureBoxes') 信息,即。行号、列号和图像索引。 tile class 具有名为 Row、Column 和 TileType 的属性。我正在尝试为嵌套循环内的 属性 赋值。

//Tile Class
public class Tile : PictureBox
{
   private int row;
   private int column;
   private int tileType;
   public int Row{get; set;}
   public int Column { get; set; }
   public int TileType { get; set; }
}
public Tile[,] LoadPictureBoxes(string filename)
{
  Tile[,] tile;
  //Read the file 
  if (System.IO.File.Exists(filename) == true)
  {
    StreamReader load = new StreamReader(filename);
    int nRow = int.Parse(load.ReadLine());
    int nCol = int.Parse(load.ReadLine());
    //declare two dimensional array of Tile Reference
    tile = new Tile[nRow, nCol];
    //using nested loop to read each tile's information
    for(int i=0; i < nRow; i++)
    {
      for(int j=0; j < nCol; j++)
      {
        tile[i, j].Row = int.Parse(load.ReadLine());   //gets an exception here
        tile[i, j].Column = int.Parse(load.ReadLine());  //gets an exception here
        tile[i, j].TileType = int.Parse(load.ReadLine());  //gets an exception here
      }
    }
    load.Close();
    return tile;
  }

  return new Tile[0, 0];
}

private void loadToolStripMenuItem_Click(object sender, EventArgs e)
{
  DialogResult r = dlgLoad.ShowDialog();

  switch (r)
  {
  case DialogResult.Cancel:
    break;
  case DialogResult.OK:
    string fileName = dlgLoad.FileName;
    Tile[,] tile = LoadPictureBoxes(fileName);
    int leftPos = 100;
    int topPos = 0;
    int height = 100;
    int width = 100;
    int rowCount = 0;
    int columnCount = 0;
    //loop after each row of picturebox is generated
    for (int rowNumber = 0; rowNumber < tile.GetLength(0); ++rowNumber)
    {
      ++rowCount;
      for (int colNumber = 0; colNumber < tile.GetLength(1); ++colNumber)
      {
        ++columnCount;
        Tile pic = new Tile();
        pic.Left = leftPos;
        pic.Top = topPos;
        pic.Height = height;
        pic.Width = width;
        pic.BorderStyle = BorderStyle.Fixed3D;
        pic.SizeMode = PictureBoxSizeMode.StretchImage;
        tile[rowCount, columnCount] = pic;
        pnlLoadGame.Controls.Add(pic);
        leftPos += width;
      }
      topPos += height;
    }
    pnlLoadGame.Size = new Size(100 * rowCount, 100 * columnCount);
    pnlGameScore.Left = 100 + 100 * columnCount;
    break;
  }
}

问题:我在运行时遇到异常:System.NullReferenceException:'Object reference not set to an instance of an object.' 是否有其他方法可以为自定义 PictureBox 的 属性 赋值?

出现你的问题是因为即使你将数组定义为tile = new Tile[nRow, nCol];,你仍然需要在使用它们之前初始化它的元素。像这样:

tile[i, j] = new Tile();

之后,再做这三行就安全了:

tile[i, j].Row = int.Parse(load.ReadLine());
tile[i, j].Column = int.Parse(load.ReadLine());  
tile[i, j].TileType = int.Parse(load.ReadLine());