【发布时间】:2019-05-01 19:36:18
【问题描述】:
我正在创建一个维护学生分数的程序。我创建了一个名为students 的类,它存储数据并将其显示在列表框中。一旦用户单击添加一个新表单 (frmAddStudent) 就会加载,允许他们按名称和分数添加用户,并将其显示在主表单的列表框中。它还允许更新/删除功能。我可以成功地将学生添加到列表中并对其进行编辑,但是当我在更新学生表单中按确定按钮时,我收到了错误
System.ArgumentOutOfRangeException: '索引超出范围。必须是非负数且小于集合的大小。参数名称:index'
我查了一下,这意味着当参数的值超出被调用方法定义的允许值范围时,它会被抛出,但不确定它是如何应用在这里的。我更新时输入的值在范围内。
源码如下
https://github.com/Triptonix/Student.git
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
public List<Student> studentList = new List<Student>();
public Student GetStudent(int id) //Get student index
{
return studentList[id];
}
public void UpdateStudentList()
{
students.DataSource = null;
students.DataSource = studentList;
students.DisplayMember = "Name";
}
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;
}
学生.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>();
}
}
frmUpdateStudent
public partial class frmUpdateStudent : Form
{
private Form1 parentForm; //main form
private Student studentToEdit; //student list
private int index; //index
public frmUpdateStudent(Form1 parentForm, int index) //update parent form (Form1) with the new student and scores
{
this.parentForm = parentForm;
this.index = index;
studentToEdit = this.parentForm.GetStudent(index);
InitializeComponent();
StudentName.Text = studentToEdit.Name;
UpdateScoreDisplay();
}
public void AddScoreToStudent(int value) //add score to current student and display in the list
{
studentToEdit.AddScore(value);
UpdateScoreDisplay();
}
public void UpdateScoreAtIndex(int id, int value) //update a score selected from the list
{
studentToEdit.GetScores()[id] = value;
UpdateScoreDisplay();
}
public int GetScoreAtIndex(int id) //get the score index
{
return studentToEdit.GetScoreAt(id);
}
private void UpdateScoreDisplay() //update the score display list
{
CurrentScores.DataSource = null;
CurrentScores.DataSource = studentToEdit.GetScores();
}
private void AddScoreButton_Click(object sender, EventArgs e) //open the add score form
{
frmAddScore addScoreForm = new frmAddScore(this);
addScoreForm.Show();
}
private void RemoveScoreButton_Click_1(object sender, EventArgs e) //remove a score from current index and update display list
{
studentToEdit.GetScores().RemoveAt(CurrentScores.SelectedIndex);
UpdateScoreDisplay();
}
private void ClearScoresButton_Click_1(object sender, EventArgs e) //clear all scores
{
studentToEdit.DestroyScores();
UpdateScoreDisplay();
}
private void CloseButton_Click_1(object sender, EventArgs e)
{
Close(); //close form
}
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();
}
private void UpdateScoresButton_Click(object sender, EventArgs e)
{
frmUpdateScore updateScoreForm = new frmUpdateScore(this, CurrentScores.SelectedIndex);
updateScoreForm.Show();
}
}
【问题讨论】:
-
缺少一些细节。
parentForm.UpdateStudent(index, Form1);- 我们看不到index的来源,也看不到问题所在的UpdateStudent方法。在该方法中,您尝试使用索引从集合中获取项目,但该索引表示集合中不存在的位置。在代码中的那个位置设置一个断点。查看您正在使用的索引值,然后查看集合中有多少项目。这应该为您指明正确的方向。 -
我会添加更新方法,但我也包含了源代码以节省一些空间。
-
只包含相关代码很重要。这样人们就可以看到问题所在的部分,而无需下载其他任何东西。这也有助于保持问题的独立性......但我们仍然缺少
parentForm.UpdateStudent。它正在调用该方法,这就是异常所在,但我没有看到该方法。 -
它位于 frmUpdateStudent 的顶部。我加了。
-
我在此页面的文本中搜索
UpdateStudent。这里没有称为UpdateStudent的方法。
标签: c#