简体   繁体   English

我如何在main函数中使用这个类? 字典和索引器(集合)

[英]How do i use this class in the main function? Dictionaries and indexers (Collections)

I am trying to add entries in dictionary array list but i don't know which arguments to set in the People Class in the main function. 我试图在字典数组列表中添加条目,但我不知道在main函数中的People类中设置哪些参数。

public class People : DictionaryBase
{
    public void Add(Person newPerson)
    {
        Dictionary.Add(newPerson.Name, newPerson);
    }

    public void Remove(string name)
    {
        Dictionary.Remove(name);
    }

    public Person this[string name]
    {
        get
        {
            return (Person)Dictionary[name];
        }
        set
        {
            Dictionary[name] = value;
        }
    }
}
public class Person
{
    private string name;
    private int age;

    public string Name
    {
        get
        {
            return name;
        }
        set
        {
            name = value;
        }
    }
    public int Age
    {
        get
        {
            return age;
        }
        set
        {
            age = value;
        }
    }
}

using this seem to give me error 使用这似乎给我错误

static void Main(string[] args)
{
People peop = new People();
peop.Add("Josh", new Person("Josh"));
}

Error 2 No overload for method 'Add' takes 2 arguments 错误2方法“添加”没有重载需要2个参数

This peop.Add("Josh", new Person("Josh")); 这个peop.Add("Josh", new Person("Josh"));

should be this 应该是这个

   var josh = new Person() // parameterless constructor.
   {
        Name = "Josh" //Setter for name.
   };
   peop.Add(josh);//adds person to dictionary. 

The class People has the method Add which only takes one argument: a Person object. People类具有Add方法,它只接受一个参数:一个Person对象。 The Add on the people class method will take care of adding the it to the dictionary for you and supplying both the name (string) argument and the Person argument. Add on the people类方法将负责将它添加到字典中,并提供name(string)参数和Person参数。

Your Person class only has a parameterless constructor, which means that you need to set your Name in the setter. 您的Person类只有一个无参数构造函数,这意味着您需要在setter中设置Name。 You can do this when you instantiate the object like above. 您可以在实例化上述对象时执行此操作。

For your design this would solve the problem: 对于您的设计,这将解决问题:

    public class People : DictionaryBase
    {
        public void Add(string key, Person newPerson)
        {
            Dictionary.Add(key , newPerson);
        }

        public void Remove(string name)
        {
            Dictionary.Remove(name);
        }

        public Person this[string name]
        {
            get
            {
                return (Person)Dictionary[name];
            }
            set
            {
                Dictionary[name] = value;
            }
        }
    }
    public class Person
    {
        private string name;
        private int age;

        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }
        public int Age
        {
            get
            {
                return age;
            }
            set
            {
                age = value;
            }
        }
    }

And in Main: 在主要:

People peop = new People();
peop.Add("Josh", new Person() { Name = "Josh" });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我何时使用索引器,以及如何知道如何编写它们 - When Do I use indexers, and how to know how to write them 在词典和集合上自动添加索引器是一个很好的设计决策吗? - Are auto-adding indexers on Dictionaries and Collections a good design decision? 在多个类中使用索引器时,应如何使用属性以及类的结构应如何 - How should I use properties and what should be structure of my class for using indexers across multiple classes 在客户类别中的属性上使用索引器 - Use of indexers on properties in a customer class 如何使用具有扩展方法的索引器具有参数和函数调用 - How to use indexers with Extension Methods having out parameter and function calls 如何使用 function 在同一个 class 中编辑 Main() 中的值? - How do I edit a value in Main() with a function in the same class? C#如何在用户输入中使用字典和列表? - C# How do I use dictionaries and lists with user input? 我如何加入两个控件集合以与foreach语句一起使用 - How do I join two control collections for use with foreach statement 我如何知道何时使用堆栈而不是其他集合? - How do I know when to use a Stack instead of other Collections? 如何在另一个类中使用扩展功能? C# - How do I use an extension function in another class? C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM