How duplicate an object in a list and update property of duplicated objects ?

Posted by user359706 on Stack Overflow See other posts from Stack Overflow or by user359706
Published on 2010-06-06T14:03:43Z Indexed on 2010/06/06 14:12 UTC
Read the original article Hit count: 111

Filed under:
|

Hello

What would be the best way to duplicate an object placed in a list of items and change a property of duplicated objects ?

I thought proceed in the following manner: - get object in the list by "ref" + "article" - Cloned the found object as many times as desired (n times) - Remove the object found - Add the clones in the list

What do you think?

A concrete example:

Private List<Product> listProduct;
listProduct= new List<Product>();

Product objProduit_1 = new Produit;

objProduct_1.ref = "001";
objProduct_1.article = "G900";
objProduct_1.quantity = 30;

listProducts.Add(objProduct_1);

ProductobjProduit_2 = new Product;

objProduct_2.ref = "002";
objProduct_2.article = "G900";
objProduct_2.quantity = 35;

listProduits.Add(objProduct_2);

desired method:

public void updateProductsList(List<Product> paramListProducts,Produit  objProductToUpdate, int32 nbrDuplication, int32 newQuantity){       
...      
}

Calling method example:

 updateProductsList(listProducts,objProduct_1,2,15);

Waiting result:

Replace follow object :

ref = "001";
article = "G900";
quantite = 30;

By:

ref = "001";
article = "G900";
quantite = 15;

ref = "001";
article = "G900";
quantite = 15;

The Algorithm is correct? Would you have an idea of the method implementation "updateProductsList"

Thank you in advance for your help.

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET