473,327 Members | 2,069 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,327 software developers and data experts.

Removing items from a Hashtable in a loop?

Jen
The usual trick to get around the "collection was modified" problem by
looping through the collection backwards by index won't work with a
Hashtable because there is no way to access by index, right? How do I loop
through a Hashtable to selectively remove items?
May 19 '07 #1
5 11228
Jen wrote:
The usual trick to get around the "collection was modified" problem by
looping through the collection backwards by index won't work with a
Hashtable because there is no way to access by index, right? How do I loop
through a Hashtable to selectively remove items?
Save keys that should be removed in an ArrayList while looping
through and remove all keys from that after.

Arne
May 20 '07 #2

"Jen" <no**@nowhere.comwrote in message
news:O$**************@TK2MSFTNGP05.phx.gbl...
The usual trick to get around the "collection was modified" problem by
looping through the collection backwards by index won't work with a
Hashtable because there is no way to access by index, right? How do I
loop through a Hashtable to selectively remove items?
Here are some examples.

http://www.thescripts.com/forum/thread256383.html
May 20 '07 #3
Jen
Yikes. Couldn't the designers of the collection classes have handled this
better??
"Mr. Arnold" <MR. Ar****@Arnold.comwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
>
"Jen" <no**@nowhere.comwrote in message
news:O$**************@TK2MSFTNGP05.phx.gbl...
>The usual trick to get around the "collection was modified" problem by
looping through the collection backwards by index won't work with a
Hashtable because there is no way to access by index, right? How do I
loop through a Hashtable to selectively remove items?

Here are some examples.

http://www.thescripts.com/forum/thread256383.html


May 20 '07 #4
List has a very nice RemoveAll function which you can pass a delegate
which tells which ones to remove and which ones to keep. I don't see
such a function in Dictionary however. Too bad, they should have added that.

Jesse

* Jen wrote, On 20-5-2007 5:53:
Yikes. Couldn't the designers of the collection classes have handled this
better??
"Mr. Arnold" <MR. Ar****@Arnold.comwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
>"Jen" <no**@nowhere.comwrote in message
news:O$**************@TK2MSFTNGP05.phx.gbl...
>>The usual trick to get around the "collection was modified" problem by
looping through the collection backwards by index won't work with a
Hashtable because there is no way to access by index, right? How do I
loop through a Hashtable to selectively remove items?
Here are some examples.

http://www.thescripts.com/forum/thread256383.html


May 20 '07 #5
Apparently, I answered that question a long time ago (and I am a little
ashamed of it, because there are a few obvious errors in it).

If you need to clear all elements, call the Clear method.

If you need to clear only specific elements, you can do this:

public static int RemoveAll(Hashtable hashtable, Predicate<DictionaryEntry>
predicate)
{
// The number of items removed.
int retVal = 0;

// Create space for the dictionary entries.
DictionaryEntry[] dictionaryEntries = new
DictionaryEntry[hashtable.Count];

// Copy the keys.
hashtable.CopyTo(dictionaryEntries, 0);

// Iterate through the keys.
foreach (DictionaryEntry dictionaryEntry in dictionaryEntries)
{
// If the predicate is true, remove the item.
if (predicate(dictionaryEntry))
{
// Remove the item.
hashtable.Remove(dictionaryEntry.Key);

// Increment the count.
retVal ++;
}
}

// That's all folks.
return retVal;
}

The good thing about this is that in Orcas, you could place a "this" in
front of the Hashtable parameter and it will then be an extension method.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Mr. Arnold" <MR. Ar****@Arnold.comwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
>
"Jen" <no**@nowhere.comwrote in message
news:O$**************@TK2MSFTNGP05.phx.gbl...
>The usual trick to get around the "collection was modified" problem by
looping through the collection backwards by index won't work with a
Hashtable because there is no way to access by index, right? How do I
loop through a Hashtable to selectively remove items?

Here are some examples.

http://www.thescripts.com/forum/thread256383.html

May 20 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: microb0x | last post by:
I'm trying to remove a number of items selected in a mulitselect list box. the code For Each varSel In lstAvailableFiles.ItemsSelected lstAvailableFiles.RemoveItem (varSel) Next varSel
2
by: Mike | last post by:
Hi! I implemeted Forms colection described in this link: http://support.microsoft.com/default.aspx?scid=kb%3Ben-us%3B815707 I need to loop through the collectionbase and based on form's tag...
3
by: Filippo P. | last post by:
Hi there, I have a menu (Collection) that needs to be trimmed based on security access of the logged user. protected void AdjustMenuBasedOnUserSecurity(Items ItemsList) { foreach (Item i in...
0
by: Frawls | last post by:
Hi, This is concerned with System.Web.UI.WebControls.DropDownList I am having problems creating a method which will remove list items from a preloaded dropdownlist. This dropdown is loaded...
2
by: Milsnips | last post by:
hi there, i have the following HTML code: -------------------------- <asp:DropDownList id="hddList" runat="server"> <asp:ListItem Value="1">Item 1</asp:ListItem> <asp:ListItem Value="2">Item...
7
by: David Lozzi | last post by:
Howdy, I have a shopping cart in my arraylist. Works great for adding items but I'm displaying the shopping cart in a gridview and the user can update the qty of the items. If they set it to 0,...
17
by: Eric_Dexter | last post by:
def simplecsdtoorc(filename): file = open(filename,"r") alllines = file.read_until("</CsInstruments>") pattern1 = re.compile("</") orcfilename = filename + "orc" for line in alllines: if not...
0
by: mharness | last post by:
Hello, Under most circumstances I don't have any trouble removing items from a dropdownlist but this time it's not working. I have a ddl that list items which, when choosen are added to a...
0
by: vivek kapile | last post by:
Title: Javascript for Adding & Removing Items from a one Listbox to another Author: Vivek Kapile Email: snipped Language: JavaScript Platform: Javascript in ASP.net...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.