Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Reassign object in foreach loop c#

Not sure I understand why I can do this with a for loop and not a foreach loop?

This is the code that works. Looping through a BindingList Products, finding a match and then assigning that product at index i to the new product that’s passed in.

 public static void UpdateProduct(int productToUpdateID, Product productToUpdate)
        {
            

            for (int i = 0; i < Products.Count; i++)
            {
                if (Products[i].ProductID == productToUpdateID)
                {
                    Products[i] = productToUpdate;
                }
            }
        }
            

If I try to do this with a foreach loop I get an error that I cannot assign to the iterator variable. What is the reasoning for this and is there a way to get around it or is using a for loop for this kind of problem the best solution?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

This is essentially what I’m trying to do.

          public static void UpdateProduct(int productToUpdateID, Product productToUpdate)
        {

            foreach(Product product in Products)
            {
                if (product.ProductID == productToUpdateID)
                {
                    product = productToUpdate;
                }
            }
        }
            

I can do something like this and reassign all the properties explicitly but want to see if there is another way to do it.

            foreach(Product product in Products)
            {
                if (product.ProductID == productToUpdateID)
                {
                    product.Name = productToUpdate.Name;
                    
                }
            }

Thanks!

>Solution :

The foreach construct is for when you want to do something with each item in the list. That does not seem to be what you are doing. You are modifying the list itself, by changing removing an item and replacing it.

Personally I would not use a loop at all, I’d just remove the old item and add the new one.

public static void UpdateProduct(int productToUpdateID, Product productToUpdate)
{
    Products.RemoveAll( x => x.ProductID == productToUpdateID );
    Products.Add( productToUpdate );
}

Or if you wish to preserve order:

public static void UpdateProduct(int productToUpdateID, Product productToUpdate)
{
    var index = Products.FindIndex( x => x.ProductID == productToUpdateID );
    Products[index] = productToUpdate;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading