Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 清点物品清单。限制可能添加的对象_C#_Winforms_List_Find_Directive - Fatal编程技术网

C# 清点物品清单。限制可能添加的对象

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

我有两张名单:小组和学生。在组列表中,每个组都有一个ID,当添加新学生时,您可以从组合框中选择In。 我需要为每组分配2到4名学生

我不知道如何限制一组学生的数量

    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。