Queue[,] inventqueue = new Queue[10,7];
for(int row = 0; row < inventqueue.GetLength(0); row++)
{
for (int col = ; col < inventqueue.GetLength(1); col++)
{
if(inventqueue[row,col].Count != 0)
{
MessageBox.Show("Theres a queue on " + row + "," + col);
}
}
}
I have been trying this out but visual studio is giving me the error "Object reference not set to an instance of an object."
解决方案
You're allocating only the double array, you still need to allocate Queues for each entry in the array like:
Queue[,] inventqueue = new Queue[10,7];
for(int row = 0; row < inventqueue.GetLength(0); row++)
{
for (int col = ; col < inventqueue.GetLength(1); col++)
{
inventqueue[row,col] = new Queue();
}
}
本文介绍了一种常见的C#编程错误,即在初始化Queue类型的二维数组时未正确分配每个元素的空间,导致运行时报错。通过示例代码展示了如何为每个数组位置正确分配Queue实例。
1130

被折叠的 条评论
为什么被折叠?



