Ошибка работы с массивом: использование локальной переменной которой не присвоено значение - C#
Формулировка задачи:
private void button2_Click(object sender, EventArgs e)
{
DialogResult result = openFileDialog1.ShowDialog();
if (result == DialogResult.OK)
{
string file = openFileDialog1.FileName;
try
{
string[] text = File.ReadAllLines(file);
string s1, s2;
foreach (var item in File.ReadAllLines(file))
{
var t = item.Split(':');
s1 += t[0];
s2 += t[1];
textBox1.Text = s1;
textBox2.Text = s2;
}
}
catch (IOException exception)
{
MessageBox.Show(exception.Message);
} s1 += t[0];
s2 += t[1];Решение задачи: «Ошибка работы с массивом: использование локальной переменной которой не присвоено значение»
textual
Листинг программы
class TextFromFile
{
string[] text;
int index = 0;
public TextFromFile()
{
OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == DialogResult.OK)
{
string fileName = ofd.FileName;
try
{
text = File.ReadAllLines(fileName);
}
catch (IOException exception)
{
MessageBox.Show(exception.Message);
}
}
}
public string[] GetNext()
{
try
{
return text[index++].Split(':');
}
catch (Exception exception)
{
MessageBox.Show(exception.Message);
return new string[] { string.Empty, string.Empty };
}
}
}