0

I am trying to use this method that is ran from a button click that will retrieve the select item from a listbox then remove this item from the list. Whenever i run the code i get this error: "List that this enumerator is bound to has been modified. An enumerator can only be used if the list does not change.".

I'm completely stumped.

    public void DeleteSale()
    {
        foreach (BootSale b in lstBootSales.SelectedItems)
        {

            lstBootSales.Items.Remove(b.Id);
            lstBootSales.Items.Remove(b.Date);

            DisplayAllBootSales();
        }
    }

DisplayAllBootSales();simply refreshes the listbox values afterwards

I have a List "BootSale" where I store information which is written to a listbox. I want to be able to click the item in the listbox then click delete and it will be completely removed from the system

7
  • 2
    You can't modify a collection while enumerating through it. Commented Feb 28, 2013 at 14:43
  • What does DisplayAllBootSales do? Commented Feb 28, 2013 at 14:43
  • @bash.d Display all boot sales? Commented Feb 28, 2013 at 14:51
  • @DGibbs Thanks, Captain Obvious! Commented Feb 28, 2013 at 14:51
  • 1
    Ask a stupid question, get a stupid answer. Commented Feb 28, 2013 at 14:53

5 Answers 5

2

do this:

class Foo {

    BootSaleList bootsalelist;

    public void DeleteSale()
    {
        foreach (BootSale b in lstBootSales.SelectedItems.OfType<BootSale>().ToArray())
        {
            //temp.Remove(b); -- no more of this guy
            bootsalelist.ReturnList().Remove(b);
            lstBootSales.Items.Remove(b);

            // and no more of these guys:

            //lstBootSales.Items.Remove(b.Id); 
            //lstBootSales.Items.Remove(b.Date);
            //DisplayAllBootSales();
        }
    }

 }

where:

[Serializable]
public class BootSaleList : IDisplay
{
    private List<BootSale> bootsales;

    public List<BootSale> ReturnList()
    {
        return bootsales;
    }
}

and it will work perfectly

Sign up to request clarification or add additional context in comments.

10 Comments

thanks for help tried this but get the error "Cannot convert type 'System.Windows.Forms.ListViewItem' to 'Bootsale_project.BootSale'"
Code now runs fine but it simply deletes the content of the listbox and not the actual list
And that is precisely what yours would've done if it would not have crashed with the exception...
I apologise I maybe wasnt clear at first I have a List "BootSale" where I store information which is written to a listbox. I want to be able to click the item in the listbox then click delete and it will be completely removed from the system.
I have to ask how it is possible that SelectedItems which is a subset of Items contain things whose properties (Id and Date) are contained in Items (for which it would be a good idea to delete them from Items). I really thought you were asking about the error "List that this enumerator is bound to has been modified. An enumerator can only be used if the list does not change." and that your code was just a sample... Is that error gone or not ? Are you asking how to actually do it ? Look at my latest edit. Personally I really don't understand the removing of .Id and .Date...
|
2

The error message says it all. You can't remove items from a list you are iterating over. You must remove the items outside of the foreach loop. One simple fix is to use a regular for loop instead.

Comments

1

Best way to do this is looping the list Top - Down (instead of down - top)

for(int i = YourListItems.Count(); i > 0; i--)
{
    // Remove from list
}

This way will prevent any index errors.

Goodluck

Comments

0

You cannot modify a collection from within a foreach, you could however use a temporary list, something along the lines of:

public void DeleteSale()
{
    List<T> temp = lstBootSales;
    foreach (BootSale b in lstBootSales.SelectedItems)
    {
        temp.Items.Remove(b.Id);
        temp.Items.Remove(b.Date);

        DisplayAllBootSales();
    }

    lstBootSales = temp;
}

Comments

0

Looking on your code I might assume, that lstBootSales holds a list of BootSale. But you are trying to delete something different than BootSale. Also use for instead of foreach:

        for (int i = 0; i < lstBootSales.SelectedItems.Count; i++)
        {
            lstBootSales.Items.Remove(lstBootSales.SelectedItems[i]);

            DisplayAllBootSales();
        }

Comments

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.