我想知道如何在for循环之外访问数组。
string[] lines = File.ReadAllLines("Customers/" + listBox1.SelectedItem.ToString() + "/" + listBox1.SelectedItem.ToString() + ".txt");
for (int i = 0; i < 4; i++)
{
string[] linesSplitted = lines[i].Split(':');
}
TextboxName.Text = linesSplitted[0];
TextboxAddress.Text = linesSplitted[1];
TextboxZip.Text = linesSplitted[2];
TextboxTel.Text = linesSplitted[3];
TextboxEmail.Text = linesSplitted[4];
此时,它无法识别分割线。
答案 0 :(得分:1)
根据你对另一个答案的评论,我猜你想要这个:
string[] linesSplitted = new string[5];
for (int i = 0; i < 5; i++)
{
linesSplitted[i] = lines[i].Split(':')[1];
}
如果这不是您想要的,请举例说明文本文件的内容。
答案 1 :(得分:0)
在循环外声明数组。 E.g。
string[] lines = File.ReadAllLines("Customers/" + listBox1.SelectedItem.ToString() + "/" + listBox1.SelectedItem.ToString() + ".txt");
string[] linesSplitted;
for (int i = 0; i < 4; i++)
{
linesSplitted = lines[i].Split(':');
}
TextboxName.Text = linesSplitted[0];
TextboxAddress.Text = linesSplitted[1];
TextboxZip.Text = linesSplitted[2];
TextboxTel.Text = linesSplitted[3];
TextboxEmail.Text = linesSplitted[4];
答案 2 :(得分:0)
string[] lines = File.ReadAllLines("Customers/" + listBox1.SelectedItem.ToString() + "/" + listBox1.SelectedItem.ToString() + ".txt");
string[] linesSplitted;
for (int i = 0; i < 4; i++)
{
linesSplitted = lines[i].Split(':');
}
TextboxName.Text = linesSplitted[0];
TextboxAddress.Text = linesSplitted[1];
TextboxZip.Text = linesSplitted[2];
TextboxTel.Text = linesSplitted[3];
TextboxEmail.Text = linesSplitted[4];
我不确定你在那里想要达到的目标......
答案 3 :(得分:0)
string[] lines = File.ReadAllLines("Customers/" + listBox1.SelectedItem.ToString() + "/" + listBox1.SelectedItem.ToString() + ".txt");
List<string[]> data = new List<string[]>();
for (int i = 0; i < 4; i++)
{
data.Add(lines[i].Split(':'));
}
//Retrive array from list and value from array and set to text box
TextboxName.Text = linesSplitted[0];
TextboxAddress.Text = linesSplitted[1];
TextboxZip.Text = linesSplitted[2];
TextboxTel.Text = linesSplitted[3];
TextboxEmail.Text = linesSplitted[4];