471,091 Members | 1,526 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,091 software developers and data experts.

Removing item from NameValueCollection

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
3 9086
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
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
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.

Similar topics

6 posts views Thread by JohnK | last post: by
reply views Thread by sameer mowade via .NET 247 | last post: by
6 posts views Thread by A.M | last post: by
7 posts views Thread by PJ6 | last post: by
3 posts views Thread by Walter Zydhek | last post: by
7 posts views Thread by =?Utf-8?B?Sm9lbCBNZXJr?= | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.