Home > Software design >  C# Change value on List within class
C# Change value on List within class

Time:12-05

it may looks some stupid but i stopped coding for a while. I have a class 'employee' looks like this:

    public class Employee
    {
        public string Name { get; set; }
        public List<task> tasks {get; set;}

        public Employee(string name, List<task> ListTasks)
        {
            Name = name;
       
            tasks = new List<task>();
           //tasks=ListTasks;
            tasks.AddRange(ListTasks);
        }
    }

and a class of task:

    public class task
    {
        public string Titel { get; set; }
        public int duration { get; set; } = 0;
        public int maxDuration { get; set; }
    }

after the creation of 2 (employee-)objects i try to add them to a empList and change the duration of 1 employes (task-)duration, but i always change both, what am i doing wrong ?

 private static void Main(string[] args)
    {

   
        var taskList = new List<task> { 
            new task
        {
            Titel = "Bumm",
            maxDuration = 4
        }, 
            new task
        {
            Titel = "Kaban",
            maxDuration = 8
        } };


        Employee bla1 = new Employee("werner", taskList);
        Employee bla2 = new Employee("ecki", taskList);

        var empListe = new List<Employee>
        {
            bla1,
            bla2
        };
        // bla2.tasks[1].Dauer = 2;
        empListe[1].tasks[1].duration = 2;
        foreach (var name in empListe)
        {
            Console.WriteLine(name.Name);
            foreach (var line in name.tasks)
            {
                Console.WriteLine("{0}: {1}/{2}",line.Titel, line.duration, line.maxDuration);
            }
        }
    }

i tried differnt ways of initiaon and linking the taskList, I wanted to change the value of only 1 one employees taskList

CodePudding user response:

    Employee bla1 = new Employee("werner", taskList);
    Employee bla2 = new Employee("ecki", taskList);

Both employees reference the same list. Passing the list does not copy it. The list only exists once. Changing it will be reflected in both employees, which reference the same list.

If you want independent lists, create two separate lists with separate instances as elements:

    var taskList1 = new List<task> { 
        new task
    {
        Titel = "Bumm",
        maxDuration = 4
    }, 
        new task
    {
        Titel = "Kaban",
        maxDuration = 8
    } };

    var taskList2 = new List<task> { 
        new task
    {
        Titel = "Bumm",
        maxDuration = 4
    }, 
        new task
    {
        Titel = "Kaban",
        maxDuration = 8
    } };

    Employee bla1 = new Employee("werner", taskList1);
    Employee bla2 = new Employee("ecki", taskList2);

CodePudding user response:

@knittl is correct - this is a reference issue - the lists are the same list, not copies of each other.

One way to fix it might look like this:

        Employee bla1 = new Employee("werner", new List<task>(taskList));
        Employee bla2 = new Employee("ecki", new List<task>(taskList));
  • Related