Click here to Skip to main content
15,892,746 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
public class Employee
{
    public byte ID { get; set; }
    public string Name { get; set; }
}
public class Task 
{
    public int ID { get; set; }
    public string Description { get; set; }


    private  List<Employee> employeeTask ;
    public  List<Employee> EmployeeTask
    {
            get
            {
                if (employeeTask == null)
                {
                    employeeTask = //Any Source changeable ;
                }
                return employeeTask; 
            }
    }
}
class Program
{
    void main()
    {
         List<Task> ListTask = //fill with groub of Tasks;
                               /*When Call 
                               property EmployeeTask in every Object in list (ListTask), 
                               the property(EmployeeTask) in all Object 
                               have the Same content (have last content element)*/
     }
}

How i can make every object in ListTask have Content of its own( property EmployeeTask )??
Posted

1 solution

Just complete the class Task. Either add the getter to the property EmployeeTask or add a constructor accepting the value of the type List<Employee> to initialize the value of this property.

However, the whole design is questionable even now when the code is incomplete. You need to property encapsulate the list of tasks. Never leave the field employeeTask uninitialized, initialize it at the point of declaration with an empty list. Optionally, provide a list interface for the list without exposing the list itself.

—SA
 
Share this answer
 
Comments
Anderso0on 2-May-13 0:21am    
i initialize List<employee> from source (data base), i get initialize when return list of employee from data base ,what about my qustion
(How i can make every object in ListTask have Content of its own( property EmployeeTask ))??
Sergey Alexandrovich Kryukov 2-May-13 0:25am    
You don't have a property "Content", so I assume this is EmployeeTask. I answered to it in first paragraph. Add instances of Task one by one, with initialized EmployeeTask for each.
—SA
Anderso0on 2-May-13 0:29am    
OK, how i can do that in my code
Anderso0on 2-May-13 0:42am    
ooooh , thanks alot :) :) You have saved me
Sergey Alexandrovich Kryukov 2-May-13 0:43am    
My pleasure. Good luck,
—SA

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