-5
可能重複:
use unassigned local variable 'multidimension'讀取文件多維數組C#分裂
我得到一個錯誤,當我運行這個程序。代碼所做的是從文本文件中讀取行和句子之間用逗號隔開,我所做的是將它們拆分並放入多維數組中,但運行時出現錯誤:
"unhanded exception has occurred in your application if you click continue the application will ignore this error and attempt to continue if you click quit the application will close immediately"
代碼:
try {
DialogResult result = openFileDialog1.ShowDialog();
if (result == DialogResult.OK) {
using (StreamReader sr = new StreamReader(openFileDialog1.FileName)) {
string[] data= null;
string ReadFromReadLine;
ReadFromReadLine = sr.ReadLine();
string[,] multidimensional = new string[ReadFromReadLine.Length, data.Length];
while (ReadFromReadLine != null) {
data = ReadFromReadLine.Split(',');
for (int i = 0; i <= ReadFromReadLine.Length; i++) {
for (int j = 0; j <= data.Length; j++) {
multidimensional[i, j] = data[j];
}
}
}
for(int i = 0 ; i<ReadFromReadLine.Length;i++) {
for(int j = 1; j<= data.Length ; j++) {
textBox1.Text += multidimensional[i,j];
}
}
}
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");
}
}
**你有什麼例外**? _使用調試器!_ – SLaks
@SLaks:這是他對[使用未分配的局部變量'multidimension'](http://stackoverflow.com/questions/7669026/use-unassigned-local-variable-multidimension)的後續。到目前爲止,調試仍然存在四個問題。 – user7116
您已發佈三個關於同一問題的問題。這個答案:http://stackoverflow.com/questions/7669026/use-unassigned-local-variable-multidimension/7669130#7669130包含解決您的問題。主要問題是當你不知道你有多少行時,你正在嘗試構建一個多維數組。他建議使用「列表」是一個很好的建議。 –