2016-11-02 64 views
4

我有一个组合框,其中包含studentList的学生。当我选择一个学生时,它应该填充学生姓名的文本字段。每当学生从组合框中选择我收到以下错误如何解决索引超出范围错误?

ArgumentOutOfRangeException was unhandled 
Index was out of range. Must be non-negative and less than the size of the collection. 

我觉得问题可能出在我的循环,但我无法找出如何修正这个错误,任何帮助,将不胜感激

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    int i; 
    for (i = 0; i < Main.studentList.Count; i++) 
    { 
     if (comboBox1.SelectedItem == Main.studentList[i].StudentName + " " + Main.studentList[i].StudentId) 
     {     
      break; 
     } 
    } 

    txtName.Text = Main.studentList[i].StudentName; //where the error occurs 
} 

public void ChangeStudent_Load(object sender, EventArgs e) 
{ 
    //loading combobox from studentList 
    foreach (var student in Main.studentList) 
    { 
     comboBox1.Items.Add(student.StudentName + " " + student.StudentId); 
    } 
} 
+2

您是否尝试过调试应用程序?在发生错误的行上放置一个断点 - 然后检查“i”的值。 –

+3

您正在循环外使用循环变量('i')。这很少是一个好主意。你认为'我'有什么价值?在循环之后? – MAV

回答

7

它抛出一个错误被突破后,我就会增的原因。如果我是列表中的最后一项,现在它已超出范围。如果不是,它现在指向下一个项目。

简单的解决方案是将错误发生在断点上方的线移动;

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    int i; 
    for (i = 0; i < Main.studentList.Count; i++) 
    { 
     if (comboBox1.SelectedItem == Main.studentList[i].StudentName + " " + Main.studentList[i].StudentId) 
     {     
      txtName.Text = Main.studentList[i].StudentName; 
      break; 
     } 
    } 
} 

此外,请考虑使用foreach循环。这与foreach循环完全相同。它使它更具可读性。

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    foreach (var student in Main.studentList) 
    { 
     if (comboBox1.SelectedItem == student.StudentName + " " + student.StudentId) 
     {     
      txtName.Text = student.StudentName; 
      break; 
     } 
    } 
} 
1

您正在使用相同的变量(i)进行范围计算和赋值。 试试这个。因为在最后的循环的for循环i递增1发生

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) 
     { 
      int j; 
      for (int i = 0; i < Main.studentList.Count; i++) 
      { 
       if (comboBox1.SelectedItem == Main.studentList[i].StudentName + " " + Main.studentList[i].StudentId) 
       { 
    j=i;     
        break; 
       } 

      } 

      txtName.Text = Main.studentList[j].StudentName; //where the error occurs 

     } 

     public void ChangeStudent_Load(object sender, EventArgs e) 
     { 
      //loading combobox from studentList 
      foreach (var student in Main.studentList) 
      { 
       comboBox1.Items.Add(student.StudentName + " " + student.StudentId); 

      } 
     } 
+0

如果找不到匹配项会发生什么情况?它不再抛出异常,但行为现在是否正确?如果学生列表为空,会发生什么? –

6

错误:

i++ 

然后,它计算为false在表达式:

i < Main.studentList.Count 

所以当你到达发生错误的那一行I等于Main.studentList.Count,所以发生索引超出范围的错误。

如果要访问列表的最后一个元素,你可以这样做:如果你要评估在每次循环语句只是将它你在for循环中

Main.studentList[Main.studentList.Count - 1].StudentName 

或者:

for (int i = 0; i < Main.studentList.Count; i++) 
{ 
    if (comboBox1.SelectedItem == Main.studentList[i].StudentName + " " + Main.studentList[i].StudentId) 
    { 
     txtName.Text = Main.studentList[i].StudentName;    
     break; 
    } 
} 

这也有将变量i保留在循环范围内的优势。

3

更改代码这样的,请再试一次

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    int i; 
    for (i = 0; i < Main.studentList.Count; i++) 
    { 
     if (comboBox1.SelectedItem == Main.studentList[i].StudentName + " " + Main.studentList[i].StudentId) 
     {    
      txtName.Text = Main.studentList[i].StudentName; 
      break; 
     } 
    } 
}