Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/337.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#_List_Combobox_Boolean - Fatal编程技术网

C# 在检查对象是否已存在时向组合框添加列表

C# 在检查对象是否已存在时向组合框添加列表,c#,list,combobox,boolean,C#,List,Combobox,Boolean,我对编程比较陌生,所以请原谅我问了这么简单的问题哈哈。 嗯,基本上我有用户可以创建的组名。Group是一个字段为“string NameGroup”的类。组名填写在文本框中,并保存在此处: List<Group> Groups = new List<Group>(); 第一个根本不起作用,第二个在文本框中输入相同的组名后给我一个错误 在检查了groupname是否存在之后,我想将它添加到combobox中,但这似乎也不起作用 if (containsItem == f

我对编程比较陌生,所以请原谅我问了这么简单的问题哈哈。 嗯,基本上我有用户可以创建的组名。Group是一个字段为“string NameGroup”的类。组名填写在文本框中,并保存在此处:

List<Group> Groups = new List<Group>(); 
第一个根本不起作用,第二个在文本框中输入相同的组名后给我一个错误

在检查了groupname是否存在之后,我想将它添加到combobox中,但这似乎也不起作用

if (containsItem == false)
{

    Groups.Add(new Group(txtNewGroup.Text));
    txtNewGroup.Clear();

}
else
{
    MessageBox.Show("Groupname isn't available, try something else");
    txtNewGroup.Clear();
}

cbxGroups.DataSource = Groups.ToString();
这是课程组:

    class Group
{
    //Fields
    private string nameGroup;


    //Properties
    public string NameGroup
    {
        get;
        set;
    }



    //Constructor
    public Group(string name)
    {
        this.nameGroup = name;

    }
 }
因此,我的问题是: 1.我的胸部有什么毛病? 2.为什么不将列表内容添加到组合框中?

此行

bool Check= Groups.ToString().Contains(txtNewGroup.Text);
由于
是一个
列表
,尽管您的组包含
字符串
,但这并不正确,因为它只会返回
列表
类型
到字符串

另外,当你说
包含(txtNewGroup.Text)
这应该是指向
类中的
字符串
,然后是
类本身,否则它几乎可能是错误的(除非
本身是
字符串

因此,假设您有这样的
类(在我的程序中,它被命名为
DummyClass
——因为我不知道您的
是什么样的)

并将其初始化为

DummyClass dc1 = new DummyClass(1, "This dummy", 2.0);
DummyClass dc2 = new DummyClass(2, "That dummy", 2.0);
DummyClass dc3 = new DummyClass(1, "These dummies", 2.0);
DummyClass dc4 = new DummyClass(2, "Those dummies", 2.0);
DummyClass dc5 = new DummyClass(3, "The dummies", 2.0);
List<DummyClass> dummyList = new List<DummyClass>() { dc1, dc2, dc3, dc4, dc5 };
同样,你的应该是

bool Check = Groups.Select(g => g.NameGroup).Contains(txtNewGroup.Text);
通过这种方式,您可以真正检查
组中是否有任何
,其
StringMember
已与
txtNewGroup.Text相同

编辑:

您的
属性还有另一个问题

class Group
{
    //Fields
    private string nameGroup;

    //Properties
    public string NameGroup
    {
        get; //There is nothing in the property, and it is never used either!
        set;
    }

    //Constructor
    public Group(string name)
    {
        this.nameGroup = name; //this means the field is updated    
    }
 }
这导致
名称
仅保存在
名称组
中,而不保存在
名称组
中。因此,当在这个类之外调用
NameGroup
时,它总是不给出任何结果。将上述类更改为:

class Group
{
    //Fields
    private string nameGroup;

    //Properties
    public string NameGroup
    {
        get {return nameGroup;} //this way, your nameGroup and NameGroup are one
        set {nameGroup = value;}
    }

    //Constructor
    public Group(string name)
    {
        this.nameGroup = name; //this means the field is updated    
    }
 }
这样,您的
nameGroup
nameGroup
就是一体。或者,如果您根本不需要该字段,可以将其删除

class Group
{
    //Properties
    public string NameGroup { get; set; }

    //Constructor
    public Group(string name)
    {
        this.NameGroup = name; //use the property directly
    }
 }

它应该可以工作

它会产生什么错误?第一个不会产生任何错误,但也不起作用。第二个:发生了“System.NullReferenceException”类型的未处理异常。我按照您的建议更改了bool,但仍然不起作用。Groups.Select(Group=>Group.NameGroup).Contains(txtNewGroup.Text)@AniBankai我编辑了我的答案。另请参阅我的评论。我相应地对其进行了更改,但仍无法将名称保存到列表中。bool Checks=Groups.Select(group=>group.NameGroup).Contains(txtNewGroup.Text);问题似乎出在别的地方。在这种情况下,也许您可以尝试更新您首先实现上述方法时得到的结果?此外,请张贴你如何保存在第一位的名称。如果可能,也显示
类是什么。我将名称保存在class Group的字符串字段中。我在帖子中添加了这个类。
class Group
{
    //Fields
    private string nameGroup;

    //Properties
    public string NameGroup
    {
        get; //There is nothing in the property, and it is never used either!
        set;
    }

    //Constructor
    public Group(string name)
    {
        this.nameGroup = name; //this means the field is updated    
    }
 }
class Group
{
    //Fields
    private string nameGroup;

    //Properties
    public string NameGroup
    {
        get {return nameGroup;} //this way, your nameGroup and NameGroup are one
        set {nameGroup = value;}
    }

    //Constructor
    public Group(string name)
    {
        this.nameGroup = name; //this means the field is updated    
    }
 }
class Group
{
    //Properties
    public string NameGroup { get; set; }

    //Constructor
    public Group(string name)
    {
        this.NameGroup = name; //use the property directly
    }
 }