473,419 Members | 1,564 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,419 software developers and data experts.

Remove Item from hashtable

How do I go about looping through a hash table and removing items. I know
how do this in a collectionbase, but can't iterate through the hash table
with out getting an error.

Here is my sample code for a collection base
for(int i = this.Values.Count; i > 0 ; i--)

{

//Set local object

MyObject aObject = this.ItemByIndex(i)

if (aObject .Type== aType)

this.Remove(aObject );

}
Nov 16 '05 #1
2 24581
MFRASER,

If you want to remove items from a Hashtable in a loop, you must use the
"for" statement, not the "foreach" statement. The reason for this is that
trying to modify the collection while enumerating through it will throw an
exception.

That being said, you can get the keys, and enumerate through those, and
remove the items, like this:

// Get the keys collection, and copy to an array.
object keys[] = new object[hashtable.Count - 1];

// Copy.
hashtable.Keys.CopyTo(keys, 0);

// A key in the collection.
object key;

// Cycle through the collection backwards.
for (int index = keys.Length - 1; index >= 0; --index)
{
// Remove the item.
hashtable.Remove(keys[index]);
}

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"MFRASER" <mf*****@henwoodenergy.com> wrote in message
news:ef**************@TK2MSFTNGP12.phx.gbl...
How do I go about looping through a hash table and removing items. I know
how do this in a collectionbase, but can't iterate through the hash table
with out getting an error.

Here is my sample code for a collection base
for(int i = this.Values.Count; i > 0 ; i--)

{

//Set local object

MyObject aObject = this.ItemByIndex(i)

if (aObject .Type== aType)

this.Remove(aObject );

}

Nov 16 '05 #2
Agreed.

I think your first line of code should read "hashtable.Count" not
"hashtable.Count - 1"

And perhaps one could still use the more concise foreach. e.g.:

object keys[] = new object[hashtable.Keys.Count];
hashtable.Keys.CopyTo(keys, 0);

foreach(object key in keys)
{
hashtable.Remove(key);
}

Felix

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in message news:<#H**************@TK2MSFTNGP10.phx.gbl>...
MFRASER,

If you want to remove items from a Hashtable in a loop, you must use the
"for" statement, not the "foreach" statement. The reason for this is that
trying to modify the collection while enumerating through it will throw an
exception.

That being said, you can get the keys, and enumerate through those, and
remove the items, like this:

// Get the keys collection, and copy to an array.
object keys[] = new object[hashtable.Count - 1];

// Copy.
hashtable.Keys.CopyTo(keys, 0);

// A key in the collection.
object key;

// Cycle through the collection backwards.
for (int index = keys.Length - 1; index >= 0; --index)
{
// Remove the item.
hashtable.Remove(keys[index]);
}

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"MFRASER" <mf*****@henwoodenergy.com> wrote in message
news:ef**************@TK2MSFTNGP12.phx.gbl...
How do I go about looping through a hash table and removing items. I know
how do this in a collectionbase, but can't iterate through the hash table
with out getting an error.

Here is my sample code for a collection base
for(int i = this.Values.Count; i > 0 ; i--)

{

//Set local object

MyObject aObject = this.ItemByIndex(i)

if (aObject .Type== aType)

this.Remove(aObject );

}

Nov 16 '05 #3

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

Similar topics

6
by: Jules Winfield | last post by:
I'm often in a situation where I have a Hashtable full of objects. I'm then presented with an object and *if* that object exists in the Hashtable, I need to remove it from the table. I therefore...
1
by: Andrew Chen | last post by:
Hi: I've tried to use Hashtable.Item property, which should return a value according to the key assigned. But it returns an error saying the function does not exist in the class (it did not...
1
by: Thomas Wang | last post by:
i declare a hashtable in c#, i can not find "hashtable.item" property as the specification declared, What i can do? B.Rgds
2
by: Anony | last post by:
Hi All, I used the Hashtable.Item in VB.Net, i.e. Dim Tables As New Hashtable Tables.Item(ID) = objTable Now, I tried the same in C#, and got an error says the Hashtable has no Item...
4
by: Anders Borum | last post by:
Hello! I'm am currently working on making a central cache component threadsafe, and was looking at the synchronized implementation of the Hashtable. I was wondering why you'd really want to...
9
by: Merlin | last post by:
Hi, My code below doesn't work does anyone have any pointers? All my controls are programically added. Dim i As Int16 For i = 0 To Me.Controls.Count - 1 If Me.Controls(i).Name <>...
2
by: Shen | last post by:
Hi, I have an array/arraylist of string items. What I want is to find out which item repeats itself most often. Any simple example for that? Thanks in advance. sean
18
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the...
10
by: pamelafluente | last post by:
Hi I have a sorted list with several thousands items. In my case, but this is not important, objects are stored only in Keys, Values are all Nothing. Several of the stored objects (might be a...
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
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...

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.