C# - 部分代码中创建的列表无法从其余部分访问 [英] C# - list created in part of code can't be accessed from the rest

查看:73
本文介绍了C# - 部分代码中创建的列表无法从其余部分访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我在C#中进行编码并刚刚开始,我的第一个评估是从CSV文件中获取数据并将其加载到列表中(必须使用列表),然后将其加载到我的dataGridView中Windows窗体应用程序,然后让它可编辑。现在我有一个不错的想法,一旦我在dataGridView中有它,但我很困惑这个列表一旦我创建并返回它就无法访问。我回来时做错了什么,或者错过了一步?



如果我在返回列表时设置了一个断点,它可以正常工作,我的来自CSV文件的40行数据都被安排到他们的列表项中正确排序,但是一旦我离开那段代码,它就不会显示出来。



最后,我的计划一旦我将数据输入到这个数据表中是将我的dataGridView上的源设置为dataTable,因为一堆在线论坛帖子已经说过找到了作品,显然我还没有写它来添加所有不同的项目但是首先我要弄清楚为什么列表不是' t出现在intellisense或工作中。在此先感谢。



Okay, so I'm coding in C# and just starting out, and my first assessment is to take data from a CSV file and load it onto a list (must use a list) and then load that into a dataGridView in my Windows Forms App and then have it be editable. Now I have a decent idea how to do it once I have it in the dataGridView but I'm seriously confused about this list not being accessible once I create and return it. Am I doing something seriously wrong when returning, or missing a step??

If I set a break-point right on returning the list, it works fine and my 40 lines of data from the CSV file are all arranged into their list items and properly sorted, but once I get out of that bit of code, it just doesn't show up.

Finally, my plan once I get the data into this dataTable is to set the source on my dataGridView to the dataTable as a bunch of online forum posts have said works find, and obviously I haven't written it to add all the different items yet but first I need to figure out why the list isn't showing up in intellisense or working at all. Thanks in advance.

public static IList<DailyValues> ReadValues(string path)
        {
            var myList = new List<DailyValues>();
            foreach (var line in File.ReadLines(path).Skip(1))
            {
                myList.Add(DailyValues.FromLine(line));
            }
            return myList;
        }

        public static DataTable dt()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("ItemCode");
            dt.Columns.Add("ItemDescription");
            dt.Columns.Add("CurrentCount");
            dt.Columns.Add("OnOrder");
            foreach (var ItemCode in myList)
            {
                dt.Rows.Add(new object[] { ItemCode.ItemCode });
            }
            return dt;
        }





我的尝试:



这只是当前的迭代,我尝试了一堆完全不起作用的东西。



What I have tried:

this is just the current iteration, I tried a bunch of stuff that completely didn't work.

推荐答案

查看你的代码示例, myList 变量不在 dt()方法的范围内。您需要从 ReadValues(...)方法传递结果并将其传递给 dt()方法。 ..类似

Looking at your code sample, the myList variable is not in the scope of your dt() method. You need to pass the result from ReadValues(...) method and pass it to your dt() method ... something like
public static DataTable dt(IList myList)
{
 // code goes here
}



然后致电:


Then to call:

var table = dt(ConvertCSVtoDataTable(inFilePath));


这是一段代码,展示了如何将CSV文件导入DataTable。改变做你想要的第一次评估并不需要太多。

Here is a working piece of code that shows how to get a CSV file into a DataTable. It won't take much to alter to do what you want for your "first assessment".
public static DataTable ConvertCSVtoDataTable(string strFilePath)
{
    DataTable dt = new DataTable();
    using (StreamReader sr = new StreamReader(strFilePath))
    {
        string[] headers = sr.ReadLine().Split(',');
        foreach (string header in headers)
        {
            dt.Columns.Add(header);
        }
        while (!sr.EndOfStream)
        {
            string[] rows = sr.ReadLine().Split(',');
            DataRow dr = dt.NewRow();
            for (int i = 0; i < headers.Length; i++)
            {
                dr[i] = rows[i];
            }
            dt.Rows.Add(dr);
        }
    }
    return dt;
}


Quote:

C# - 创建的列表部分代码无法从其余部分访问

C# - list created in part of code can't be accessed from the rest



您需要学习'变量范围'。根据您的要求,您声明变量的地方很重要。

3.7范围(C#) [ ^ ]



更正式的定义是范围是一个封闭的上下文或区域定义可以无限制地使用名称。

了解类和对象C#Way |面向对象编程| InformIT [ ^ ]



Visual C#2010 - 可变范围 - YouTube [ ^ ]


You need to learn 'Variables scopes'. Depending on what you want to do, the place where you declare a variable matters.
3.7 Scopes (C#)[^]

"A more formal definition is that scope is an enclosing context or region that defines where a name can be used without qualification."
Understanding Classes and Objects the C# Way | Object-Oriented Programming | InformIT[^]

Visual C# 2010 - Variable Scope - YouTube[^]

这篇关于C# - 部分代码中创建的列表无法从其余部分访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆