我想知道我传递给函数的数字是否包含在我拥有的任何列表中。这是代码的摘录。我已经初始化了列表网格,正如这里的许多类似帖子中所说的那样,但这不起作用。
错误说
“System.NullReferenceException:'未将对象引用设置为对象的实例。'”
指网格。我知道这似乎是重复的,但我已经检查了与此类似的其他帖子,但找不到针对我的问题的具体答案。
public partial class MainWindow : Window
{
//This list is much bigger
public List<int> grid1 = new List<int>();
public List<int> grid2 = new List<int>();
public List<int> grid3 = new List<int>();
public List<int> grid4 = new List<int>();
public List<int> grid5 = new List<int>();
public MainWindow()
{
InitializeComponent();
}
private bool numberValide(int number, int Grid) // Grid {1..5}
{
List<int> = new List<int>();
grid = (List<int>)this.FindName("grid" + Grid);
if (grid != null)
{
if (!grid.Contains(number))
{
grid.Add(number); //the error is here
return true;
}
else
{
return false;
}
}
else
{
grid.Add(number); //error also here
return true;
}
}
}
尚方宝剑之说
慕斯709654
相关分类