使用未分配的局部变量“多维”;

发布于 2024-12-08 12:56:50 字数 1714 浏览 0 评论 0原文

我在下面的代码中收到错误使用未分配的局部变量“多维”。我试图将从文本文件返回的数据拆分到多维数组中,并将每一行放入数组中

    private void button1_Click_1(object sender, EventArgs e)
    {
        string[,] Lines;
        //string[][] StringArray = null;
        //to get the browsed file and get sure it is not curropted
        try 
        {
            DialogResult result = openFileDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                using (StreamReader sr = new StreamReader(openFileDialog1.FileName))
                {
                    string[] data= null;
                    string ReadFromReadLine;

                    while ((ReadFromReadLine = sr.ReadLine()) != null)
                    {
                        data = ReadFromReadLine.Split(',');
                        for (int i = 0; i <= ReadFromReadLine.Length; i++)
                        {
                            for (int j = 0; j <= data.Length; j++ )
                            {
                                string[,] multidimensional;
                                multidimensional[i, j] = data[j];

                            }
                        }                  

                    }
                    //foreach(string s in Lines)
                    //{
                    //    EditItemComboBox.Items.Add(s);
                    //}

                }
                FilePath.Text = openFileDialog1.FileName;
               //textBox1.Text += (string)File.ReadAllText(FilePath.Text);
            }
        }
        catch(IOException ex) 
        {

            MessageBox.Show("there is an error" + ex+ "in the file please try again");
        } 
    }

任何想法我做错了什么?

I am getting an error use unassigned local variable 'multidimension' from below code. I am trying to put the data returned back from the text file in a multidimensional array by splitting them and put each row in in the array

    private void button1_Click_1(object sender, EventArgs e)
    {
        string[,] Lines;
        //string[][] StringArray = null;
        //to get the browsed file and get sure it is not curropted
        try 
        {
            DialogResult result = openFileDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                using (StreamReader sr = new StreamReader(openFileDialog1.FileName))
                {
                    string[] data= null;
                    string ReadFromReadLine;

                    while ((ReadFromReadLine = sr.ReadLine()) != null)
                    {
                        data = ReadFromReadLine.Split(',');
                        for (int i = 0; i <= ReadFromReadLine.Length; i++)
                        {
                            for (int j = 0; j <= data.Length; j++ )
                            {
                                string[,] multidimensional;
                                multidimensional[i, j] = data[j];

                            }
                        }                  

                    }
                    //foreach(string s in Lines)
                    //{
                    //    EditItemComboBox.Items.Add(s);
                    //}

                }
                FilePath.Text = openFileDialog1.FileName;
               //textBox1.Text += (string)File.ReadAllText(FilePath.Text);
            }
        }
        catch(IOException ex) 
        {

            MessageBox.Show("there is an error" + ex+ "in the file please try again");
        } 
    }

Any Ideas what I am doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

断念 2024-12-15 12:56:50
string[,] multidimensional;

应该是:

string[,] multidimensional = new string[ReadFromReadLine.Length, data.Length];

并移出 for 循环,并可能发送到方法、缓存或其他东西

string[,] multidimensional;

should be:

string[,] multidimensional = new string[ReadFromReadLine.Length, data.Length];

and moved out of the for loops and maybe sent to a method, cached, or something

心凉 2024-12-15 12:56:50

您只是定义了一个名为“多维”的数组,但没有将其分配给任何内容。

for (int j = 0; j <= data.Length; j++ )
{
    string[,] multidimensional = new String[i,data.Length]
    multidimensional[i, j] = data[j];
}

但是,我不确定我是否遵循您在最内层循环中尝试执行的操作。每次循环遍历 data 中的元素时,您都会定义一个名为“多维”的新数组,并且每次都会丢失旧数据。

如果假设“多维”包含整个文件的内容,则需要在第一个循环之外定义它,但要像这样使用数组,您需要知道文件中的行数。如果您使用的是 C#2 或更高版本,则列表<>会是一个更好的选择

var list = new List<String[]>();

while ((ReadFromReadLine = sr.ReadLine()) != null)
{
    data = ReadFromReadLine.Split(',');
    list.Add(data);        
}

You are just defining an array called 'multidimensional', but not assigning it to anything.

for (int j = 0; j <= data.Length; j++ )
{
    string[,] multidimensional = new String[i,data.Length]
    multidimensional[i, j] = data[j];
}

However, I am not sure I am following what you are trying to do in the innermost loop. You are defining a new array called 'multidimensional' each time you are looping through the elements in data and the old data is lost each time.

If 'multidimensional' is suppose to contain the contents of the entire file, you need to define it outside of the first loop, but to use an array like you are, you need to know the number of lines in your file. If you are using C#2 or greater, a List<> would be a better choice

var list = new List<String[]>();

while ((ReadFromReadLine = sr.ReadLine()) != null)
{
    data = ReadFromReadLine.Split(',');
    list.Add(data);        
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文