如何解决索引超出范围的错误?

本文关键字:范围 错误 索引 何解决 解决 | 更新日期: 2023-09-27 18:16:02

我有一个组合框,其中包含来自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);
    }
}

如何解决索引超出范围的错误?

抛出错误的原因是在break之后,i被增加了。如果i是列表中的最后一项,那么它就越界了。如果不是,它现在指向下一项。

简单的解决方案是将抛出错误的行移到换行符上方;

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

发生错误是因为在for循环的最后一个循环中i增加了1:

i++

然后在表达式中求值为false:

i < Main.studentList.Count

当你到达错误发生的那一行时,I等于Main.studentList.Count,因此出现了索引超出范围的错误。

如果你想访问列表的最后一个元素,你可以这样做:

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

或者,如果您想在每个循环中计算语句,只需将其移动到for循环中:

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保持在循环范围的局部。

像这样修改代码,请再试一次

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

您正在使用相同的变量(i)进行范围计算和分配。试试这个。

 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);
            }
        }