Click here to Skip to main content
15,893,487 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm creating a program that maintains student scores. I've created a class called students that stores the data and displays it in a list box. Once the user clicks btnAdd a new form (frmAddStudent) loads that allows them to add the user by name and their scores and display it in the listbox in the main form. It also allows the update/delete functions. I can succesfully add students to the list and edit them, but when I press the ok button in the update students form I get the error, "System.ArgumentOutOfRangeException: 'Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index'". I know this means that the argument passed was out of range, but I'm not sure how. Any help is appreciated. Thanks! I've included my source code if you want a deeper look.

frmUpdateStudent.cs

 private void UpdateButton_Click_1(object sender, EventArgs e) //open update form for current student
 {
 Student Form1 = new Student();
 Form1.Name = StudentName.Text;
                parentForm.UpdateStudent(index, Form1);
 Close();

 }

Form1.cs

<pre>public List<Student> studentList = new List<Student>();


public Student GetStudent(int id) //Get student index
{
return studentList[id];

}

public bool UpdateStudent(int originalIndex, Student studentToEdit)
{
try
{
Student student = GetStudent(originalIndex); //select index of student
student.Name = studentToEdit.Name; //name of student
studentList.RemoveAt(originalIndex); //remove the student at the index selected
studentList.Insert(originalIndex, student); //insert new student at index.
UpdateStudentList(); //update student list
}
catch { return false; }
return true;
}


Student.cs

public class Student
{
public List<int> Scores = new List<int>();

public string Name
{ get; set; }

public bool AddScore(int score)
{
try
{
Scores.Add(score);
}
catch { return false; }
return true;
}

public List<int> GetScores()
{
return Scores;
}

public int GetScoreAt(int index)
{
return (int)Scores[index];
}

public int GetScoreTotal()
{
int sum = 0;
foreach (int score in Scores)
{
sum += score;
}
return sum;
}

public int GetScoreCount()
{
return Scores.Count;
}

public int GetScoreAverage()
{
return GetScoreTotal() / GetScoreCount();
}

public void DestroyScores()
{
Scores = new List<int>();

}

What I have tried:

Source Code - GitHub - Triptonix/Student: App[^]
Posted
Updated 1-May-19 11:08am
v2

1 solution

You don't show the updatestudent method but obviously the index you're passing in, is invalid. Why do you think it's not?
 
Share this answer
 
Comments
Member 14351839 1-May-19 17:08pm    
I added the UpdateStuent method and because I'm entering an integer and I have it set as an integer?
Christian Graus 1-May-19 17:10pm    
The index is an integer you entered? how can that work? It's an index into an array, it should be the index of the item you're editing, passed in from the parent form
Member 14351839 1-May-19 17:16pm    
GetStudent(int id) UpdateStudent = Student student = GetStudent(originalIndex);
Member 14351839 1-May-19 17:12pm    
I'm not sure how to explain. I attached my source code so you can see the exact issue. Going to keep working at it.
Christian Graus 1-May-19 17:16pm    
It's exactly what I said

Student student = GetStudent(students.SelectedIndex); //select index from list

students.SelectedIndex is -1. I thought it might be. It's the index of the listbox. Store that in a variable when you start your edit, because it's being deselected when your new dialog appears.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900