0

I'm desperate. I have seen a lot of answers about this issue, but does not work for me.

My method takes an object type Operation and a list of machines. The object Operation has an idMachine. Now, I need create a List Operation where there are many items as number of machines in list and each element has an idMachine different. The problem is that each element have the same idMachine, and this is the last assigned.

In my first approach I did:

public void SetOperationCreatedBulk(Operation op, List<Machines> listMachines){
    List<Operation> listOp = new List<Operation>();
    foreach(var machine in listMachines) {
         op.idMachine = machine.idMachine; // assign the corresponding id
         listOp.Add(op); // add modify op to list
    }
    // do something whith listOp...
}
    

Soon I found out that it's not possible change the list value into foreach-loop and I read this answer where advises use for-loop

Then I did:

public void SetOperationCreatedBulk(Operation op, List<Machines> listMachines){
    List<Operation> listOp = new List<Operation>();
    for (int i = 0; i < listMachines.Count; i++) {
         op.idMachine = listMachines[i].idMachine;
         // listOp[i] = op; // trigger exception, although I do 'List<Operation> listOp = new List<Operation>(listMachines.Count);'
         listOp.Add(op);
    }
    // do something whith listOp...
}

I understand that the problem is the reference to object Operation. I'm working all time in the same reference. I have tried to create an auxiliar Operation object into loop but I have get the same result.

Thanks for your time.

Community
  • 1
  • 1
robBerto
  • 196
  • 2
  • 14

1 Answers1

2

If you want each list item to have its own Operation with its own idMachine, then you need to clone the incoming Operation op.

Something like:

foreach(var machine in listMachines) 
{
    // Create clone 
    var newOp = new Operation
    {
        SomeProperty = op.SomeProperty,
        // Probably map other properties
    };

    newOp.idMachine = machine.idMachine;
    listOp.Add(newOp);
}
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • Maybe you should put some more emphasis on the importance of cloning all the properties of the `op` instance. – Thorsten Dittmar Apr 28 '15 at 11:55
  • @Thorsten I mentioned it twice. If OP wants to learn more about (deep) cloning, there's plenty of questions about that. I'm not here to repeat existing answers. ;-) – CodeCaster Apr 28 '15 at 11:58
  • https://msdn.microsoft.com/en-us/library/system.object.memberwiseclone(v=vs.110).aspx this link provides information on Clone methods that already exist on Object. Depending on what you are trying to achieve, this may be enough. Otherwise, you could implement IClonable on the object you wish to clone, and then override the Clone method with something similar to the code in this answer – David Watts Apr 28 '15 at 12:17
  • 1
    @CodeCaster I didn't ask you to write an essay about cloning - it's just that the OP should understand he needs to copy all properties to the new instance, so basically I meant some comment like `// Other properties here` in your `// Create clone` block. I understand what you mean, but the fact that the OP already struggles creating one instance per list item rings alarm bells :-D – Thorsten Dittmar Apr 28 '15 at 13:14
  • Ok, it works fine. But now I don't like my code. Operation has about 20 attributes. Thx! – robBerto Apr 28 '15 at 13:49