我有一个班级单元:
public class Cell
{
public enum cellState
{
WATER,
SCAN,
SHIPUNIT,
SHOT,
HIT
}
public Cell()
{
currentCell = cellState.WATER;
MessageBox.Show(currentCell.ToString());
}
public cellState currentCell { get; set; }
}
然后我尝试在以下课程中使用它:
public class NietzscheBattleshipsGameModel
{
private byte MAXCOL = 10;
private byte MAXROW = 10;
public Cell[,] HomeArray;
private Cell[,] AwayArray;
public NietzscheBattleshipsGameModel()
{
HomeArray = new Cell [MAXCOL, MAXROW];
AwayArray = new Cell [MAXCOL, MAXROW];
}
public string alphaCoords(Int32 x)
{
if (x < 0 || x > 9)
{
throw new ArgumentOutOfRangeException();
}
char alphaChar = (char)('A' + x);
return alphaChar.ToString();
}
public void test()
{
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
// Object reference not set to an instance of an object.
MessageBox.Show(HomeArray[i,j].currentCell.ToString());
///////////////////////////////////////////////////////
}
}
}
}
我最终没有将对象引用设置为对象的实例(在上述代码中的 ///// 之间..
我尝试创建一个 Cell 实例,它工作正常。