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

removing an item from a dictionary

P: n/a
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 through a dictionary, looking for
things, then remove them....

John
Jul 21 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Here's one way:

Dim Keys(myDictionary.Count - 1) As Object

For i As Integer = 0 to myDictionary.Count - 1
myDictionary.Remove(Keys.GetValue(i))
Next i

hope that helps..
Imran.
"JohnK" <Jo***@discussions.microsoft.com> wrote in message
news:D7**********************************@microsof t.com...
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 through a dictionary, looking for
things, then remove them....

John

Jul 21 '05 #2

P: n/a
added info, my key to the dictionary is of type string, so I'm lost how to
interate, such that I can remove while I loop. I'm in C# if that helps

"JohnK" wrote:
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 through a dictionary, looking for
things, then remove them....

John

Jul 21 '05 #3

P: n/a
....and here's your C# version:

object[myDictionary.Count - 1] Keys;
for (int i = 0; i <= myDictionary.Count - 1; i++) {
myDictionary.Remove(Keys.GetValue(i));
}

hope that helps..
Imran.

"JohnK" <Jo***@discussions.microsoft.com> wrote in message
news:E4**********************************@microsof t.com...
added info, my key to the dictionary is of type string, so I'm lost how
to
interate, such that I can remove while I loop. I'm in C# if that helps

"JohnK" wrote:
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 through a dictionary, looking for
things, then remove them....

John

Jul 21 '05 #4

P: n/a
Imran Koradia <no****@microsoft.com> wrote:
...and here's your C# version:

object[myDictionary.Count - 1] Keys;
for (int i = 0; i <= myDictionary.Count - 1; i++) {
myDictionary.Remove(Keys.GetValue(i));
}


That's a bad way of doing it, IMO (and won't even compile in its
current form). There's no guarantee that Keys will maintain the
original order when items are removed from it.

I would suggest creating a collection to contain the keys you wish to
remove, and after you've found everything you want to remove, you then
remove them:

ArrayList removals = new ArrayList();

foreach (object key in myDictionary.Keys)
{
if (someCondition)
{
removals.Add(key);
}
}

foreach (object key in removals)
{
dictionary.Remove(key);
}

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

P: n/a
ok..looks like I was half asleep. I forgot the CopyTo in the original VB
piece of which I then simply translated using one of the online tools. And
yes - it doesn't even compile - my apologies :(.

So here's the code again in VB:

Dim Keys(myDictionary.Count - 1) As Object
myDictionary.Keys.CopyTo(Keys, 0)

For i As Integer = 0 To myDictionary.Count - 1
myDictionary.Remove(Keys(i))
Next

The C# code looks like (which now compiles and also runs :)):
object[] Keys = new object[myDictionary.Count];
myDictionary.Keys.CopyTo(Keys, 0);
int count = myDictionary.Count;
for (int i = 0; i <= count-1; i++) {
myDictionary.Remove(Keys[i]);
}

Of course, you can always add conditions before removing the items. You
should be able to get the Item by myDictionary.Item(Keys[i]) if you need to
check a condition on the item before removing it.
hope that helps..
Imran.

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Imran Koradia <no****@microsoft.com> wrote:
...and here's your C# version:

object[myDictionary.Count - 1] Keys;
for (int i = 0; i <= myDictionary.Count - 1; i++) {
myDictionary.Remove(Keys.GetValue(i));
}


That's a bad way of doing it, IMO (and won't even compile in its
current form). There's no guarantee that Keys will maintain the
original order when items are removed from it.

I would suggest creating a collection to contain the keys you wish to
remove, and after you've found everything you want to remove, you then
remove them:

ArrayList removals = new ArrayList();

foreach (object key in myDictionary.Keys)
{
if (someCondition)
{
removals.Add(key);
}
}

foreach (object key in removals)
{
dictionary.Remove(key);
}

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

Jul 21 '05 #6

P: n/a
thanks everyone, that was helpful
John
Jul 21 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.