How can I solve an index error from a range?

I have a combo box in which students from a student participate. When I select a student, he must fill in the text box for the studentโ€™s name. Whenever a student selects from a combo box, I get the following error:

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

I think the problem may be in my cycle, but I am having problems finding a bug fix, any help would be appreciated

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);
    }
}
+4
source share
4 answers

The reason she throws a mistake is that after the break, I get an increment. If I was the last item on the list, it is now out of scope. If it is not, now it points to the next element.

A simple solution would be to move the line that throws an error above break;

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

, 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 .

+6

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

You use the same variable (i) to calculate the range and destination. Try it.

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

            }
        }
+1
source

Source: https://habr.com/ru/post/1659618/


All Articles