C# 使用未分配的局部变量';多维';
我从下面的代码中得到一个错误,使用未分配的局部变量“多维”。我试图将从文本文件返回的数据拆分成多维数组,并将每一行放入数组中C# 使用未分配的局部变量';多维';,c#,multidimensional-array,unassigned-variable,C#,Multidimensional Array,Unassigned Variable,我从下面的代码中得到一个错误,使用未分配的局部变量“多维”。我试图将从文本文件返回的数据拆分成多维数组,并将每一行放入数组中 private void button1_Click_1(object sender, EventArgs e) { string[,] Lines; //string[][] StringArray = null; //to get the browsed file and get sure it is n
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");
}
}
private void按钮1\u单击1(对象发送者,事件参数e)
{
字符串[,]行;
//字符串[][]StringArray=null;
//获取已浏览的文件并确保其未被选中
尝试
{
DialogResult=openFileDialog1.ShowDialog();
if(result==DialogResult.OK)
{
使用(StreamReader sr=newstreamreader(openFileDialog1.FileName))
{
字符串[]数据=null;
字符串ReadFromReadLine;
而((ReadFromReadLine=sr.ReadLine())!=null)
{
data=ReadFromReadLine.Split(',');
对于(int i=0;i
应该是:
string[,] multidimensional = new string[ReadFromReadLine.Length, data.Length];
并移出for循环,可能发送到方法、缓存或其他对象
应该是:
string[,] multidimensional = new string[ReadFromReadLine.Length, data.Length];
并移出for循环,可能发送到方法、缓存或其他对象,您只是定义了一个名为“多维”的数组,但没有将其分配给任何对象
for (int j = 0; j <= data.Length; j++ )
{
string[,] multidimensional = new String[i,data.Length]
multidimensional[i, j] = data[j];
}
for(int j=0;j您只是定义了一个名为“多维”的数组,但没有将其分配给任何对象
for (int j = 0; j <= data.Length; j++ )
{
string[,] multidimensional = new String[i,data.Length]
multidimensional[i, j] = data[j];
}
用于(int j=0;j您想在这里做什么?不仅多维
从不赋值,也从不从中读取。您想创建一个多维数组,每行一行,每行一列,每行一个值一列吗?这看起来是您想做的,但很难确定。您想做什么ng在这里要做什么?不仅从未分配多维
,也从未从中读取。您是否试图创建一个多维数组,其中每行有一行,每行上的每个值有一列?这看起来像是您要做的,但很难确定。