-3

我想知道如何在 forloop 之外访问数组。

    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];

在这一点上,它不识别linesSplitted。

4

4 回答 4

1

从您对另一个答案的评论中,我猜您想要这个:

string[] linesSplitted = new string[5];
for (int i = 0; i < 5; i++)
{
    linesSplitted[i] = lines[i].Split(':')[1];
}

如果这不是您想要的,请给我们一个文本文件内容的示例。

于 2013-06-20T10:41:05.047 回答
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];
于 2013-06-20T10:10:49.053 回答
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];

我不确定你想在那里实现什么......

于 2013-06-20T10:11:04.030 回答
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];
于 2013-06-20T10:31:31.593 回答