修改类变量时,代码将永久退出方法

时间:2014-11-11 04:12:57

标签: c#

我刚刚在Visual Studio Ultimate 2013中启动了一个新的Windows窗体项目。我有一个csv文件,我可以通过消息框警告验证它是否正确读取,但是当我尝试构建一个数组时在父类中声明,似乎代码永久退出该方法。

当前代码(缩减为相关部分)

public partial class Form1 : Form
{
    FileStream fs;
    StreamReader sr;
    public string[][] myTable;
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

        OpenFileDialog filediag = new OpenFileDialog();
        filediag.Filter = "CSV files (.csv)|*.csv";
        filediag.Multiselect = false;
        filediag.ShowDialog();
        if (filediag.FileName != null)
        {
            MessageBox.Show("opening file"); //trying to find error point
            fs = new FileStream(filediag.FileName, FileMode.Open);
            MessageBox.Show("File open. Setting up stream reader"); //trying to find error point
            sr = new StreamReader(fs);
            MessageBox.Show("stream reader setup. begining while loop"); //trying to find error point
            int i = 0;
            string line1;
            string[] line = new string[5]; //line and line1 separate due to trying to locate issue.
           // This next line is where I have issues. 
           //A debugging breakpoint will show this line happens, however, the line after is never will. 
           //Without this line, and the myTable line within the loop, 
           //the messagebox will show for every line of the csv file indicating the while loop itself also works.
            myTable = new string[File.ReadAllLines(filediag.FileName).Count()][];
            while ((line1 = sr.ReadLine()/*.Split(',')*/) != null)
            {
                MessageBox.Show("in loop. line: " + line1); //trying to find error point
                i++;
                line = line1.Split(',');
                MessageBox.Show("line split and prepared for array"); //trying to find error point
                //If the previous myTable initializer doesn't exist, 
                //this will become the last line of the program on the first pass through the loop.
                myTable[i] = line;
                MessageBox.Show("line added to array. i = " + i); //trying to find error point
            }
            MessageBox.Show("final line:" + line1);
            catLabel.Show();
            catLabel.Text = myTable[0][0];
        }

    }
}

Windows表单将在这些问题行运行后立即弹出,并且程序中的任何位置都不会显示错误。我无法弄清楚导致它的原因。任何帮助将不胜感激,谢谢。

2 个答案:

答案 0 :(得分:0)

这里的问题是你已经用

打开了文件
fs = new FileStream(filediag.FileName, FileMode.Open);

然后尝试使用File.ReadAllLines(filediag.FileName)再次打开它。也许更简单的解决方案就是使用File.ReadAllLines?例如(并在通用列表的帮助下):

var content = File.ReadAllLines(filediag.FileName);
var myTable = new List<string[]>();

foreach (var line in content)
{
    myTable.Add(line.Split(','));
}

答案 1 :(得分:0)

如果没有“初始值设定项”,则myTablenull,这就是程序退出的原因。

请注意,这是处理文件的一种非常可怕的方式 - 首先,您要读取文件两次(一次使用StreamReader,然后再次执行File.ReadAllLines)。如果您使用List而不是二维数组,则可以稍微简化程序。您还应该将您的一次性用品放入using陈述中。

我已经将代码缩减到了基本要素:

using System.Collections.Generic;

public partial class Form1 : Form
{
    // Change to a list instead of a two-dimensional array
    public IList<string[]> myTable = new List<string[]>();

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

        OpenFileDialog filediag = new OpenFileDialog();
        filediag.Filter = "CSV files (.csv)|*.csv";
        filediag.Multiselect = false;
        filediag.ShowDialog();

        if (filediag.FileName != null)
        {
            using (var fs = new FileStream(filediag.FileName, FileMode.Open))
            {
                using (var sr = new StreamReader(fs))
                {
                    string line1;

                    // myTable has been initialised so no need for initializer here.

                    while ((line1 = sr.ReadLine()/*.Split(',')*/) != null)
                    {
                        var line = line1.Split(',');
                        myTable.Add(line);
                    }

                    catLabel.Show();
                    catLabel.Text = myTable[0][0];
                }
            }
        }

    }
}