使用未分配的局部变量“多维”;
我在下面的代码中收到错误使用未分配的局部变量“多维”。我试图将从文本文件返回的数据拆分到多维数组中,并将每一行放入数组中
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
应该是:
并移出 for 循环,并可能发送到方法、缓存或其他东西
should be:
and moved out of the for loops and maybe sent to a method, cached, or something
您只是定义了一个名为“多维”的数组,但没有将其分配给任何内容。
但是,我不确定我是否遵循您在最内层循环中尝试执行的操作。每次循环遍历 data 中的元素时,您都会定义一个名为“多维”的新数组,并且每次都会丢失旧数据。
如果假设“多维”包含整个文件的内容,则需要在第一个循环之外定义它,但要像这样使用数组,您需要知道文件中的行数。如果您使用的是 C#2 或更高版本,则列表<>会是一个更好的选择
You are just defining an array called 'multidimensional', but not assigning it to anything.
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