简体   繁体   English

C#列表对象超出范围。 不确定如何处理

[英]c# list object is out of scope. Not sure how to handle it

So, I have a simple C# application. 因此,我有一个简单的C#应用​​程序。 The application lets a user enter a test score, and click Add button. 该应用程序允许用户输入测试分数,然后单击“添加”按钮。 When clicked, the text box contents (if valid) should enter into the list object. 单击时,文本框内容(如果有效)应输入到列表对象中。

My current code is saying that it the object doesn't exist in the current context. 我当前的代码是说该对象在当前上下文中不存在。

private void ScoreCalculatorApplication_Load(object sender, EventArgs e)
    {
        List<int> scoreList = new List<int>();
    }

    private void btn_Add_Click(object sender, EventArgs e)
    {
        scoreList.Add();//this line has the problem
    }

So, I am not sure why the scoreList doesnt exist, because the ScoreCalculatorApplication_Load method executes on loading the app. 因此,我不确定为什么scoreList不存在,因为ScoreCalculatorApplication_Load方法在加载应用程序时执行。

Anyway, I also considered something like this: 无论如何,我也考虑过这样的事情:

private void ScoreCalculatorApplication_Load(object sender, EventArgs e)
        {
             //remove this method.
        }

        private void btn_Add_Click(object sender, EventArgs e)
        {
           //is there a test to see if this object exists?
            if (//see if the list does not exist)
                //if not existant, create it here.
            scoreList.Add();
        }

So, The problem is I dont know how to test if the object exists. 所以,问题是我不知道如何测试对象是否存在。

The problem here is that you're creating scoreList in a more restrictive scope than btn_Add_Click exists in. You've defined it within the scope of the ScoreCalculatorApplication_Load method, which means that reference will be automatically garbage collected after the method completes, and will never be accessible outside that method. 这里的问题是,在比btn_Add_Click存在的范围更广的范围内创建了scoreList。您已经在ScoreCalculatorApplication_Load方法的范围内定义了它,这意味着引用将在方法完成后自动进行垃圾收集,并且永远不会在该方法之外可以访问。

If you want the scoreList object to be accessible to all methods within your class, you need to make a field or property. 如果希望scoreList对象可被您的类中的所有方法访问,则需要创建一个字段或属性。 Within the class scope, create and initialize a List: 在类范围内,创建并初始化一个List:

private List<int> scoreList = new List<int>();
private void ScoreCalculatorApplication_Load(object sender, EventArgs e)
{
    /// put whatever else you need to do on loading here
}

scoreList will now be accessible within any given instance of your class. 现在可以在类的任何给定实例中访问scoreList。 Note that if you need scoreList to be accessible by other objects, you should make it a public property, rather than a private field. 请注意,如果需要scoreList可供其他对象访问,则应将其设置为公共属性,而不是私有字段。

Note that there's not really a need to check whether an object "exists" in the sense you seem to mean -- the program will not compile and run if you've referenced an object or method that does not exist in that scope. 注意,实际上并不需要检查对象是否按照您的意思“存在”-如果您引用了该范围中不存在的对象或方法,程序将不会编译和运行。 If you like, you can check if the scoreList has been initialized and can be populated by checking if it is null, eg if (scoreList == null). 如果愿意,可以检查scoreList是否已初始化,并且可以通过检查是否为空来填充它,例如(scoreList == null)。

furkle answers the main issue. Furkle回答了主要问题。

Just wanted to answer your supplmentary question that now scoreList is in scope you can test if it has been initialised with if(scoreList==null) 只是想回答您的一个补充问题,即现在scoreList在范围内,您可以测试是否已使用if(scoreList==null)对其进行了初始化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM