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>();
Date - store the dateTime object
<pre> public class Date
{
public DateTime date { get; set; }
}
listClass - main function: create a list of
Person class
public class listClass
{
public List<Person> People{ get; set; } = new List<Person>();
}
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);
}
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;
p.dates.Add(d);
}
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.