C#将值添加到列表中的列表中 [英] C# add values to a list inside a list

查看:94
本文介绍了C#将值添加到列表中的列表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





之前我问过这个问题,但是我没有正确地改写它。那么让我再问这个问题。



我有三个班级。

人员 - 存储人员属性

Hi,

I have asked this question before, however I did not rephrased it correctly. So let me ask this question again.

I have three classes.
Person - store the person attributes

public string Name { get; set; }
        public string Surname { get; set; }
        public int Age { get; set; }
        public List<Date> dates{ get; set; } = new List<Date>();





日期 - 商店dateTime对象



Date - store the dateTime object

<pre>  public class Date
    {
        public DateTime date { get; set; }
    }





listClass - 主要功能:创建一个人员类的列表



listClass - main function: create a list of Person class

public class listClass
{
     public List<Person> People{ get; set; } = new List<Person>();
}





在我的主表单中,我有两个按钮AddPerson和AddDate我想要实现的是允许(通过从dateTimePicker中选择日期)将日期数添加到人员列表中的一个对象,即一个人可以有2-3个日期。



我是什么尝试过:



我已经正确添加了人物。





In my main form, I have two button AddPerson and AddDate what I'm trying to achieve is to allow (by choosing date from dateTimePicker) add number of dates to one object inside the Person list, i.e one person can have 2-3 dates.

What I have tried:

I have added person correctly.

private void SaveDriver(object sender, EventArgs e)
              {
                    Person p = new Person();
                    listClass list = new listClass();

                    p.Name = txtFirstName.Text;
                    p.Surname = txtSurname.Text;
                    p.Age = int.Parse(txtAge.Text);

                    list.People.Add(p);
              }



这是正常的,它将此人添加到与 Person class 相关的人员列表中



我不知道该怎么做,我想如何添加日期数,即一个人可以有3个日期。



我创建了一个按钮AddDate:


This works correctly, it adds the person into a person list which is related to the Person class

What I don't know how to do, is how I suppose to add number of dates into it, i.e one person can have 3 dates.

I have created a button AddDate:

private void btnAddDate_Click_1(object sender, EventArgs e)
       {
           People p = new People();
           Date d = new Date();


           d.date = dtpDate.Value; //date time picker

           p.dates.Add(d); //trying to add the dates into the People list
         }





我知道最后一段代码不正确,因为每当我点击添加日期按钮时,它都不会将日期从DateTimePicker添加到List< ;日期>在Person Class中的日期,所以我想如何允许用户为个人对象添加日期数?



提前感谢。



I know the last bit of code is incorrect, because whenever I click the add date button it does not add the date from DateTimePicker to the List<date> dates in Person Class so how I suppose to allow the user to add number of dates to a person object?

Thanks in advance.

推荐答案

嘿,在下面的表单类中创建people对象



Hey, create people object outside but within the form class like below

Public FormPeople: Window
{
 People people = new People();
 IList<People> peoples = new List<People>();
 
Public FormPeople()  {  
people.dates = new  List<Date>();
 }
private void btnAddDate_Click_1(object sender, EventArgs e)
        {
            
            Date d = new Date();            

            
            d.date = dtpDate.Value; //date time picker              
            
            people.dates.Add(d); //trying to add the dates into the People list
          }
}
private void AddPeople()
{
  peoples.add(people);
}


我假设你使用Visual Studio IDE,在项目中你可以声明类。你已经定义了这些类并不重要。
I assumed you use Visual Studio IDEand within the project you can declare classes. It doesn't matter you have already defined those classes.


这篇关于C#将值添加到列表中的列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆