Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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
C#
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
C#
<pre>  public class Date
    {
        public DateTime date { get; set; }
    }


listClass - main function: create a list of Person class
C#
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.

C#
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:
C#
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
         }


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.
Posted
Updated 24-Mar-17 14:33pm
v4
Comments
[no name] 24-Mar-17 12:17pm    
Okay so why didn't you update your previous posting?
And it's still not clear why you think that "the last bit of code is incorrect".
Member 13081540 24-Mar-17 12:31pm    
Sorry, next time I will update the questions.

I think its incorrect because whenever I click the add date button it does not add this date to the List<date> dates in Person Class
[no name] 24-Mar-17 12:45pm    
I think it does. I believe your actual problem is that you do not understand variable scope.
Member 13081540 24-Mar-17 13:00pm    
You are 100% correct, whenever I was adding claims and people the variables were inside the specific scopes rather than declared at the top of the form.
[no name] 24-Mar-17 13:14pm    
Yes and now you are creating a brand new "People" adding to it, and then throwing it all away when the event handler exits.

I assumed you use Visual Studio IDEand within the project you can declare classes. It doesn't matter you have already defined those classes.
 
Share this answer
 
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);
}
 
Share this answer
 
Comments
CHill60 24-Mar-17 19:46pm    
Where are these other classes defined though? People? It's listClass.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900