Click here to Skip to main content
14,332,651 members
Rate this:
Please Sign up or sign in to vote.
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
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; //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
NotPolitcallyCorrect 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
NotPolitcallyCorrect 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.
NotPolitcallyCorrect 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.
Member 13081540 24-Mar-17 13:19pm
   
hmm, ok. Then how could I not make it all 'throw away'?
CHill60 24-Mar-17 12:24pm
   
There is more wrong than just that last piece of code!
People p = new People()? The name of class is listClass
public List<Date> dates{ get; set; } = new List<Claim>(); generates an invalid token compile error and what is a Claim ... shouldn't that be a List<Date>??
Member 13081540 24-Mar-17 12:34pm
   
Yes sorry, its the List<Date> dates{ get; set; } = new List<Date>();
CHill60 24-Mar-17 13:46pm
   
Which will still generate an invalid token compiler error - you can't initialise properties like that.
Member 13081540 24-Mar-17 13:54pm
   
But People p = new People() is used to add new people to the List<person> people in the listClass

Dates are used to add number of dates inside a People class to the list called List<date> dates
Ramza360 24-Mar-17 15:15pm
   
That is incorrect CHill60, you can initialize properties using that syntax in C#.
CHill60 24-Mar-17 19:45pm
   
Which version of C# is that Ramza? My compiler gives the error "Invalid token '=' in class , struct, or interface member declaration".
Of course if I change that property to an encapsulated property with a backing field instead of an auto-property then of course you can use that syntax to initialize the field.
I'm using VS2013 but I'm not convinced it's ever been any different
Ramza360 28-Mar-17 9:45am
   
Its a VS 2015 thing (well C# 6.0 to be exact), but with VS 2015 you can use some of these features (including this one) with even c# 4.0.
CHill60 28-Mar-17 10:01am
   
Fair enough. I'm using VS2013
Bryian Tan 24-Mar-17 15:10pm
   
Just curious, where is the People? I mean the class :)
Rate this:
Please Sign up or sign in to vote.

Solution 1

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);
}
   
Comments
CHill60 24-Mar-17 19:46pm
   
Where are these other classes defined though? People? It's listClass.
Rate this:
Please Sign up or sign in to vote.

Solution 2

I assumed you use Visual Studio IDEand within the project you can declare classes. It doesn't matter you have already defined those classes.
   

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100