C# 清点物品清单。限制可能添加的对象
我有两张名单:小组和学生。在组列表中,每个组都有一个ID,当添加新学生时,您可以从组合框中选择In。 我需要为每组分配2到4名学生 我不知道如何限制一组学生的数量C# 清点物品清单。限制可能添加的对象,c#,winforms,list,find,directive,C#,Winforms,List,Find,Directive,我有两张名单:小组和学生。在组列表中,每个组都有一个ID,当添加新学生时,您可以从组合框中选择In。 我需要为每组分配2到4名学生 我不知道如何限制一组学生的数量 private List<Student> students; private List<Group> groups; private void buttonAdd_Click(object sender, EventArgs e) { Student stud
private List<Student> students;
private List<Group> groups;
private void buttonAdd_Click(object sender, EventArgs e)
{
Student student = new Student();
student.StID = textStudentID.Text;
student.StLName = textLastName.Text;
student.AssignStudentToGroup(comboGroupID.Text);
Group groupSelectedBox = groups.Find(x => x.GroupID == comboGroupID.Text);
if (groupSelectedBox.Count <= 4)
{
students.Add(student);
}
else
{
MessageBox.Show("Too many people");
}
ResetStudentViewGrid();
}
私人名单学生;
私人名单组;
私有无效按钮单击(对象发送者,事件参数e)
{
学生=新生();
student.StID=textStudentID.Text;
student.StLName=textLastName.Text;
student.AssignStudentToGroup(comboGroupID.Text);
groupgroupselectedbox=groups.Find(x=>x.GroupID==comboGroupID.Text);
如果(groupSelectedBox.Count如果这是添加新学生的地方,那么是的,我认为这是合适的
如果有可能在其他地方重用此代码,您可能希望将其全部放在名为的方法中,例如,AddStudentToGroup()
,并从单击事件调用该方法
此外,您还需要将if
语句更改为
if (groupSelectedBox.Count < 4)
if(groupSelectedBox.Count<4)
否则,您将允许一个组中有5名学生。@Grantwiney出于某种原因,它给了我一个错误:操作员使用count作为方法:.count()@devilfish17啊,谢谢。但现在它说,尽管我已经使用System.Linq;了,但我缺少一个指令eve。