By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,426 Members | 2,928 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,426 IT Pros & Developers. It's quick & easy.

Removing item from NameValueCollection

P: n/a
I am having a problem using the NameValueCollection type.

If I remove one of the items while iterating through an
collection of this type, I end up with an exception.
This exception is:
Collection was modified after the enumerator was
instantiated.

This happens when it attempts to continue through the
enumeration.

Is there a way around this? Is there another way to
remove items from the collection while iterating through
it?

-Walter Zydhek
Jul 21 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Walter Zydhek <wa**@giftmarket.org> wrote:
I am having a problem using the NameValueCollection type.

If I remove one of the items while iterating through an
collection of this type, I end up with an exception.
This exception is:
Collection was modified after the enumerator was
instantiated.

This happens when it attempts to continue through the
enumeration.

Is there a way around this? Is there another way to
remove items from the collection while iterating through
it?


Not using a straight foreach, no. The most common way of getting round
it is to create another collection for all the items to remove, and
then remove them all after the first iteration.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #2

P: n/a
You could iterate starting at the end using a standard "for" loop
decrementing the index for each item. When you come across an item that you
do not want, you can remove it and continue. The reason for starting at the
end is because as you remove the item, the length of the collection changes
so your loop's ending condition is no longer valid. However, the index for
the first element never changes.

"Walter Zydhek" <wa**@giftmarket.org> wrote in message
news:03****************************@phx.gbl...
I am having a problem using the NameValueCollection type.

If I remove one of the items while iterating through an
collection of this type, I end up with an exception.
This exception is:
Collection was modified after the enumerator was
instantiated.

This happens when it attempts to continue through the
enumeration.

Is there a way around this? Is there another way to
remove items from the collection while iterating through
it?

-Walter Zydhek

Jul 21 '05 #3

P: n/a
Actually, I found a better (native?) way of doing it which
makes more sense I guess.

This method is to do the for-each on a NEW instance of the
NameValueCollection passing the existing
NameValueCollection in the New Constructor. Then removing
the items from the original collection. It works great for
me.

Example:

dim a as new NameValueCollection
a.add("name-a","value-a")
a.add("name-b","value-b")
a.add("name-c","value-c")

dim item as string
for each item in New NameValueCollection(a)
if a(item) = "value-b" then
a.remove(item)
end if
next

this will result in value-b being removed, but not
interrupting the for-each loop

-Walt
-----Original Message-----
You could iterate starting at the end using a standard "for" loopdecrementing the index for each item. When you come across an item that youdo not want, you can remove it and continue. The reason for starting at theend is because as you remove the item, the length of the collection changesso your loop's ending condition is no longer valid. However, the index forthe first element never changes.

"Walter Zydhek" <wa**@giftmarket.org> wrote in message
news:03****************************@phx.gbl...
I am having a problem using the NameValueCollection type.
If I remove one of the items while iterating through an
collection of this type, I end up with an exception.
This exception is:
Collection was modified after the enumerator was
instantiated.

This happens when it attempts to continue through the
enumeration.

Is there a way around this? Is there another way to
remove items from the collection while iterating through
it?

-Walter Zydhek

.

Jul 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.