0

I have the following code:

public class BaseEmployee
{
   public bool Status {get;set;}
   public DateTime DateOfJoining {get;set;}
}

public class Employee : BaseEmployee
{
   public string Name {get;set;}
   public string City {get;set;}
   public string State {get;set;}
}



foreach(var record in records)
{
  var employee = GetDefaultBaseEmployeeProperties();
  employee.Name = record.Name
  employee.State = record.Name;
  employee.City = record.city;

  Department.Employess.Add(employee)

 }

When I do this then all the employees get updated with the same of name, city and state as the last employee added. So to get around the problem of reference I did

 Department.Employees.Add(new Employee {
        Name = record.Name;
        City = record.City;
        State = record.State;
   });

But the problem with this approach is that I loose the BaseEmployee properties in the employee object.

I need a way of adding the employee to the Department.Employees with the base properties retained. any ideas from you people, without touching the base class.

FYI: moving the base class properties to the employee class is not an option.

4
  • 2
    Post the code for GetDefaultBaseEmployeeProperties(). Commented Mar 30, 2014 at 20:31
  • no, you won't loose the BaseEmployee properties.Why would you think that ? Commented Mar 30, 2014 at 20:32
  • This should work out of the box Commented Mar 30, 2014 at 20:32
  • what type does GetDefaultBaseEmployeeProperties return Commented Mar 30, 2014 at 20:48

2 Answers 2

2

If the behavior you describe really occurs with the code you posted, there is only one conclusion:

  • GetDefaultBaseEmployeeProperties() returns the same Employee instance every time it is called.

This is bad, as you have witnessed. Fix GetDefaultBaseEmployeeProperties() to make it return a new Employee instance every time.


EDIT: If you cannot change GetDefaultBaseEmployeeProperties(), you can copy the properties as follows:

var template = GetDefaultBaseEmployeeProperties();

foreach(var record in records)
{
    var employee = new Employee();      // create a *new* Employee instance

    employee.Status = template.Status;  // copy default properties
    employee.DateOfJoining = template.DateOfJoining;

    employee.Name = record.Name;        // fill Employee with new values
    employee.State = record.State;
    employee.City = record.city;

    Department.Employees.Add(employee);
}
Sign up to request clarification or add additional context in comments.

4 Comments

i.e GetDefaultBaseEmployeeProperties should receive the current record (or bits of it) as an argument
Heinzi, MikeSW you are correct. It returns the same employee instance.Any suggestions how I can fix it in my code? I am not able to get around it. I can't change the GetDefaultBaseEmployeeProperties since its in a different project and used in other places. Any suggestions to fix my code to consume it?
If you can't change the implementation of GetDefaultBaseEmployeeProperties, you'll need to create the new employee yourself (as you already are in your fix) and copy over the base properties you care about from the result of GetDefaultBaseEmployeeProperties.
@user1527762: I extended my answer (it basically does what joshtkling just proposed).
0

Try doing this and let us know if it works:

foreach(var record in records)
{
  var temp = record; // there is sometimes a bug with fireach iterators last item.

  var employee = GetDefaultBaseEmployeeProperties();
  employee.Name = temp .Name
  employee.State = temp.State;  // you have a bug here in your original code.
  employee.City = temp.city;

  Department.Employess.Add(employee)
 }

5 Comments

there is sometimes a bug with fireach iterators last item what do you mean by that?
Read it in a Joseph albahari post.. andrusdevelopment.blogspot.com/2008/01/…
Not sure if you are hitting the same issue here, hence asked you to verify.if it didn't work then ignore my bug comment.
Then please post the GetDefaultBaseEmployeeProperties code.
There is no closure in OP code, so the case you are mentioned not an option.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.