猿问

检查多个列表 List<int> 的某个数字不起作用

我想知道我传递给函数的数字是否包含在我拥有的任何列表中。这是代码的摘录。我已经初始化了列表网格,正如这里的许多类似帖子中所说的那样,但这不起作用。


错误说


“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;

        }           

    }

}


jeck猫
浏览 117回答 2
2回答

尚方宝剑之说

您的问题的原因是FindName()通过该名称查找 XAML 元素,它不会检查类成员。所以这行代码:grid = (List<int>)this.FindName("grid" + Grid);将永远是null,因为FindName("grid" + Grid)会返回null,那么您将其转换null为 a List<int>。所以grid将是一个null List<int>在你的课堂上拥有所有这些列表是一个非常糟糕的设计。您可以使用字典来简化一些事情。我不完全确定我遵循你的逻辑是什么,但这是我尝试转换:public partial class MainWindow : Window{&nbsp; &nbsp; public Dictionary<int, List<int>> gridDictionary = new Dictionary<int, List<int>>();&nbsp; &nbsp; private bool numberValide(int number, int gridIndex)&nbsp; // Grid {1..5}&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; //Checks if dictionary has an entry at gridIndex&nbsp; &nbsp; &nbsp; &nbsp; if(gridDictionary.ContainsKey(gridIndex))&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!gridDictionary[gridIndex].Value.Contains(number))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Add number to list in dictionary&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; gridDictionary[gridIndex].Value.Add(number);&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Adds a new entry to the dictionary with a list containing number&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; gridDictionary.Add(gridIndex, new List<int>() { number });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; }请记住, Dictionaries 必须具有唯一键,因此您不能这样做:dictionary.Add(1, "one");dictionary.Add(1, "uno");这将引发异常,因为已存在重复键。对于您的用例,这应该没问题,因为无论如何您的所有grid变量都必须唯一命名才能编译。

慕斯709654

我怀疑这里的问题是您认为 FindName 会让您在类中找到一个字段。那不是它的作用。它在窗口中找到一个控件。grid1、grid2 等不是控件。他们是田野如果您想查找字段,则必须使用反射……但是,更好的方法是使用一个List<List<int>>代替。所以:List<List<int>> grids = new List<List<int>>();然后在某个地方(可能是构造函数)填充该列表:for (var i=0; i < numberOfGrids; i++)&nbsp;{&nbsp; &nbsp; grids.Add(new List<int>());}然后当你想在你的 中检索网格时numberValide:var grid = grids[Grid];&nbsp; &nbsp;// where Grid is the index to your Grid -&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // your variable names are confusing但是现在,由于您最初的问题是查看您的任何整数列表中是否存在数字,您可以使用 Linq 来大大简化这一点:var doesExist = grids.Any(g => g.Contains(number));
随时随地看视频慕课网APP
我要回答