473,387 Members | 1,592 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 9308
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: JohnK | last post by:
ok, ya got me here. I'm trying to removing items from a dictionary inside a loop. Obviously using enumeration does not work as that assumes the dictionary stays unchanged. So how so I iterate...
0
by: sameer mowade via .NET 247 | last post by:
Hello All, I have problem while dynamically removing row from the Datagrid which i have added dynamically as shown in the following code snippet. The problem is that while removing dynamically...
6
by: A.M | last post by:
Hi, When I call Request.QueryString.Clear(); ASP.NET throws me this exception: Exception Details: System.NotSupportedException: Collection is read-only If the collection is read-onle,...
7
by: PJ6 | last post by:
I thought this was going to be simple. I want to take an HttpRequest and add an item to the QueryString. First I tried to add it directly, but I got a "Collection is read-only." error. So I...
66
by: Cor | last post by:
Hi, I start a new thread about a discussion from yesterday (or for some of us this morning). It was a not so nice discussion about dynamically removing controls from a panel or what ever. It...
3
by: Walter Zydhek | last post by:
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:...
6
by: Niyazi | last post by:
Hi all, What is fastest way removing duplicated value from string array using vb.net? Here is what currently I am doing but the the array contains over 16000 items. And it just do it in 10 or...
7
by: =?Utf-8?B?Sm9lbCBNZXJr?= | last post by:
I have created a custom class with both value type members and reference type members. I then have another custom class which inherits from a generic list of my first class. This custom listneeds...
5
by: Bart Steur | last post by:
Hi, What's the best way to programmaticly select (click) another listview Item after removing the selected/focused one. using VB2005 Express. Thanks Bart
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.