473,661 Members | 2,429 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 11268
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.c omwrote in message
news:O$******** ******@TK2MSFTN GP05.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.c omwrote in message
news:%2******** ********@TK2MSF TNGP04.phx.gbl. ..
>
"Jen" <no**@nowhere.c omwrote in message
news:O$******** ******@TK2MSFTN GP05.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.c omwrote in message
news:%2******** ********@TK2MSF TNGP04.phx.gbl. ..
>"Jen" <no**@nowhere.c omwrote in message
news:O$******* *******@TK2MSFT NGP05.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(Hasht able hashtable, Predicate<Dicti onaryEntry>
predicate)
{
// The number of items removed.
int retVal = 0;

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

// Copy the keys.
hashtable.CopyT o(dictionaryEnt ries, 0);

// Iterate through the keys.
foreach (DictionaryEntr y dictionaryEntry in dictionaryEntri es)
{
// If the predicate is true, remove the item.
if (predicate(dict ionaryEntry))
{
// Remove the item.
hashtable.Remov e(dictionaryEnt ry.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.co m

"Mr. Arnold" <MR. Ar****@Arnold.c omwrote in message
news:%2******** ********@TK2MSF TNGP04.phx.gbl. ..
>
"Jen" <no**@nowhere.c omwrote in message
news:O$******** ******@TK2MSFTN GP05.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
5335
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
5491
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 either close the form or do something else. foreach(Form myForm in Forms) { if(myForm.Tag != AppConfig.homePageTitle) {
3
28698
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 ItemsList) {
0
5405
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 when the page loads and the items are pulled from a database. I want to create a method which is called on the page load and will remove certain items from the dropdown list. I want the method to loop
2
4873
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 2</asp:ListItem> <asp:ListItem Value="3">Item 3</asp:ListItem> <asp:ListItem Value="4">Item 4</asp:ListItem> <asp:ListItem Value="5">Item 5</asp:ListItem>
7
2633
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, then I want to remove it. I'm doing the following script: For Each ele In bag Dim qty As Integer = CType(gvBag.Rows.Item(cnt).FindControl("txtQty"),
17
2705
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 pattern1 print >>orcfilename, line I am pretty sure my code isn't close to what I want. I need to be able
0
1365
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 gridview on the same page and removed from the ddl so that they can only be added once. What I want to do and can't seem to make work is the code that removes the ddl items that are already on the gridview at page load.
0
7714
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 Technology: Used in ASP.net Level: Beginner Introduction This code will help for the begineers, who want to learn, how to move the the items from one list box to another listbox with up and down facility.
0
8428
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8341
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8754
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8630
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7362
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5650
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4177
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4343
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2760
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.