2010-01-27 35 views
6

如何使用数组的数值而不是用于确定大小写的数组中的数字?在我的代码:如何将数组值用于大小写切换(不是数组编号)

for (int x = 0; x < 3; x++) 
     { 
      switch (position[x]) 
      { 
       case 0: 
        label1.Text = people[x]; 
        break; 
       case 1: 
        label2.Text = people[x]; 
        break; 
       case 2: 
        label3.Text = people[x]; 
        break; 
      } 
     } 

当该运行时,它使用x在位置[],而不是定位[X]的值,用于确定要使用的情况下。例如,当x是0,但位置[x]的值是1时,它使用情况0.如何获取值?

编辑:我的代码确实是问题....出于某种原因,调试一大早具有创建虚假的图像效果...:P作为一个供参考,在这里是正确的代码...

for (int x = 0; x < 3; x++) 
     { 
      if (position[x] == 2) 
      { 
       position[x] = 0; 
      } 

      else 
      position[x]++; 

     } 

     for (int x = 0; x < 3; x++) 
     { 
      int val = position[x]; 
      switch (val) 
      { 
       case 0: 
        label1.Text = people[x]; 
        break; 
       case 1: 
        label2.Text = people[x]; 
        break; 
       case 2: 
        label3.Text = people[x]; 
        break; 
      } 

在位置[x]的上面第一次出现时,我改为只放置了x。感谢所有的帮助!

+1

什么是 '位置',然后的目的是什么? – ata 2010-01-27 14:09:37

+1

你的代码看起来很好,你确定这是问题吗? – 2010-01-27 14:09:53

+1

我没有看到这段代码有什么问题。你确定位置[]中的值是否如你所期望的那样? – Aaron 2010-01-27 14:10:46

回答

3

试试这个:

for (int x = 0; x < 3; x++) 
    { 
     int val = position[x]; 
     switch (val) 
     { 
      case 0: 
       label1.Text = people[x]; 
       break; 
      case 1: 
       label2.Text = people[x]; 
       break; 
      case 2: 
       label3.Text = people[x]; 
       break; 
     } 
    } 

也许一些容易将说:

for(int x = 0; x < 3; x++) 
{ 
    Label label = MyForm.ActiveForm.Controls["label" + position[x]] as Label; 
    if (label != null) label.Text = people[x]; 
} 
+1

这个语义和OP发布的一样。 – jason 2010-01-27 14:12:59

+0

有没有这个问题....它会工作,因此检查。 – Bloodyaugust 2010-01-27 22:29:27

相关问题