使用未分配的局部变量“多维” [英] use unassigned local variable 'multidimension'

查看:144
本文介绍了使用未分配的局部变量“多维”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到一个错误的使用来自下面的代码未分配的局部变量'多维'的。我试图把数据通过拆分他们返回从多维数组中的文本文件回来,将每行中的数组

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?

推荐答案

您只是定义称为多维数组,但它并没有使用任何东西。

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.

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

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);        
}

这篇关于使用未分配的局部变量“多维”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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