Removal of Enumerable items from a collection

Error in one case, no error in another, what's the difference?

Page 1 of 1

9 Replies - 3348 Views - Last Post: 21 October 2009 - 11:46 AM Rate Topic: -----

#1 dzone41  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 46
  • View blog
  • Posts: 216
  • Joined: 21-August 09

Removal of Enumerable items from a collection

Posted 20 October 2009 - 11:13 AM

The question has come up in another post recently about the removal of items in a collection or list using the " For Each " statement to iterate through.

This code works:
For Each item As ListViewItem In ListView1.SelectedItems
	  ListView1.Items.Remove(item)
Next



This code does not:
For Each itemChecked As Object In CheckedListBox1.CheckedItems
	CheckedListBox1.Items.Remove(itemChecked)
Next



I have been reading on this for hours today to find out why the first code block works, but the second does not.
What I've learned is the "For Each" statement implements the IEnumerable interface.
I have seen these terms used many times and scratched my head when it didn't make scense, then just moved along, something to strain my brain about later.
Today was later...
So since the IEnumerable interface is used, the .GetEnumerator method of the IEnumerable interface is called upon at the beginning of the "For Each"
block, this evaluates the collection or list as in:
For Each item As ListViewItem In myCollection


This is done at the beginning of the iteration through the myCollection, so if the myCollection is altered as in:
For Each itemChecked As Object In myCollection
	CheckedListBox1.Items.Remove(itemChecked)
Next


The altering of the list returns the error:
List that this enumerator is bound to has been modified. An enumerator can only be used if the list does not change.

Sooooo....since:
Fact 1. "For Each" statement impliments the IEnumerable interface on the collection or list

Fact 2. Once the collection or list has been evaluated by the IEnumerated.GetEnumerator at the beginning of the "For Each" block, the list can not change, or the described error will occur.

Fact 3. Something is full of poo...because this works:
For Each item As ListViewItem In ListView1.SelectedItems
	  ListView1.Items.Remove(item)
Next



and this:
For Each itemChecked As Object In CheckedListBox1.CheckedItems
	CheckedListBox1.Items.Remove(itemChecked)
Next



STILL doesn't work!!

I guess I am missing something somewhere, highly likely, completly probable, most definitely.
Any info would be appreciated

Is This A Good Question/Topic? 0
  • +

Replies To: Removal of Enumerable items from a collection

#2 MacAnimeGirl  Icon User is offline

  • D.I.C Regular

Reputation: 8
  • View blog
  • Posts: 282
  • Joined: 30-September 08

Re: Removal of Enumerable items from a collection

Posted 20 October 2009 - 11:23 AM

I would believe it would have do with the object part of the checkedlistbox.
Was This Post Helpful? 0
  • +
  • -

#3 mark.bottomley  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 176
  • View blog
  • Posts: 990
  • Joined: 22-April 09

Re: Removal of Enumerable items from a collection

Posted 20 October 2009 - 04:09 PM

The problem is that the contents are getting shorter with each removal - try using a For loop and increment BACKWARDS from .Count-1 down to 0.
Was This Post Helpful? 0
  • +
  • -

#4 dzone41  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 46
  • View blog
  • Posts: 216
  • Joined: 21-August 09

Re: Removal of Enumerable items from a collection

Posted 21 October 2009 - 08:34 AM

Let me "reiterate" the question for clarity...

Why does this code work with no errors:
For Each item As ListViewItem In ListView1.SelectedItems
	  ListView1.Items.Remove(item)
Next



and as we all know, this code produces errors:
For Each itemChecked As Object In CheckedListBox1.CheckedItems
	CheckedListBox1.Items.Remove(itemChecked)
Next



So I'm not looking for alternate code or an alternate method....I got that
Just what is the difference?

I see what MacAnimeGirl says:

Quote

I would believe it would have do with the object part of the checkedlistbox.


As this is really the ONLY difference in coding.
In comparison:


For Each item As Object In CheckedListBox1.CheckedItems
For Each item As ListViewItem In ListView1.SelectedItems

CheckedListBox1.Items.Remove(item)
ListView1.Items.Remove(item)

But the IEnumerable.GetEnumerator evaluates:
CheckedListBox1.CheckedItems and ListView1.SelectedItems
in the first line of each block...right?...no really...Is this correct??
Then does not evaluate the line again...??
It iterates through using the "Next" statement as the IEnumerator.MoveNext function.
So I would be inclined to think that no matter what the data type of the "item" as in " For Each item", block would react the same to the list change.

Searching for enlightenment......

This post has been edited by dzone41: 21 October 2009 - 08:36 AM

Was This Post Helpful? 0
  • +
  • -

#5 MacAnimeGirl  Icon User is offline

  • D.I.C Regular

Reputation: 8
  • View blog
  • Posts: 282
  • Joined: 30-September 08

Re: Removal of Enumerable items from a collection

Posted 21 October 2009 - 09:35 AM

What I meant was I think the Object is hindering it. I don't think it knows exactly was object you want.

I'm not explaining it right. Object seems out of place there.

Could you not do For Each item as Checkbox in CheckedListBox1.CheckedItems.

Somehow I don't see that working but it was just a thought.
Was This Post Helpful? 0
  • +
  • -

#6 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2216
  • View blog
  • Posts: 9,352
  • Joined: 29-May 08

Re: Removal of Enumerable items from a collection

Posted 21 October 2009 - 10:36 AM

Look at my last two tutorials on Collections, IEnumerable & IEnumerator.
Was This Post Helpful? 0
  • +
  • -

#7 MacAnimeGirl  Icon User is offline

  • D.I.C Regular

Reputation: 8
  • View blog
  • Posts: 282
  • Joined: 30-September 08

Re: Removal of Enumerable items from a collection

Posted 21 October 2009 - 10:38 AM

View PostAdamSpeight2008, on 21 Oct, 2009 - 09:36 AM, said:

Look at my last two tutorials on Collections, IEnumerable & IEnumerator.


I think this may have been your inspiration. Correct? :D :D :D
Was This Post Helpful? 0
  • +
  • -

#8 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2216
  • View blog
  • Posts: 9,352
  • Joined: 29-May 08

Re: Removal of Enumerable items from a collection

Posted 21 October 2009 - 10:42 AM

This and the previous thread. :pirate:
Was This Post Helpful? 0
  • +
  • -

#9 MacAnimeGirl  Icon User is offline

  • D.I.C Regular

Reputation: 8
  • View blog
  • Posts: 282
  • Joined: 30-September 08

Re: Removal of Enumerable items from a collection

Posted 21 October 2009 - 10:46 AM

View PostAdamSpeight2008, on 21 Oct, 2009 - 09:42 AM, said:

This and the previous thread. :pirate:

Lol, I thought so when I first saw your tutorials.
Was This Post Helpful? 0
  • +
  • -

#10 dzone41  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 46
  • View blog
  • Posts: 216
  • Joined: 21-August 09

Re: Removal of Enumerable items from a collection

Posted 21 October 2009 - 11:46 AM

Quote

Look at my last two tutorials on Collections, IEnumerable & IEnumerator.


The more I read on these .Net Interfaces, the more my question makes sense and the less the outcome of the ListView code block make sense.

Quote

I don't think it knows exactly was object you want.

The fact that the error appears, shows that the Enumerator is failing to iterate because the List has changed. In turn, the fact that the list is changing says that the "Object" was removed. This means that the code is doing exactly was is expected of it.

It's the other block of code that is puzzleing.

This block of code:
For Each item As ListViewItem In ListView1.SelectedItems
	  ListView1.Items.Remove(item)
Next


should not work, but it does
Why...how
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1