PC Review


Reply
Thread Tools Rate Thread

deleting keypair from hashtable while enumerating !??!

 
 
Tony
Guest
Posts: n/a
 
      24th Aug 2003
I have this problem - I have a hashtable, containing a list of filenames.

Every 60 seconds, I have a thread that enumerates thru this hashtable, and
based on some simple logic, some of the items in the hashtable has to be
removed.

But when I remove a pair from the hashtable, inside the enumeration loop, I
get an exception.

Why cant I remove from a hashtable, while enumerating ?

My code looks something like this :

for each r in camImages
if r.value = x then
camImages.remove(r.key)
end if
next

When the above code is running, the first time the "camImages.remove(r.key)"
line is run, I get an exception in the last line, containing "next".

Can someone explain to me what I am doing wrong ?


-
Regards,
Tony Fonager

Netcoders ApS


 
Reply With Quote
 
 
 
 
Jay B. Harlow [MVP - Outlook]
Guest
Posts: n/a
 
      24th Aug 2003
Tony,
> Why cant I remove from a hashtable, while enumerating ?

Because the enumerator (an object that implements IEnumerator, an interface
used by For Each) may 'loose' track of where it is if you delete an item,
the element the enumerator is on is no longer in the collection, so when it
goes to get the next element it has no idea where to look, as the internal
structure of the HashTable has changed. Yea, Yea, The HashTable & enumerator
could let each other know when this happens, but the 'expense' of doing this
is greater then the 'benefit'.

Generally the way to delete items while enumerating is to make a copy of the
Keys collection, then enumerator of this copy, or to add the items you want
deleted to a second collection (ArrayList for example). then delete based on
the items in this second collection. I tend to go for the second.

To make a copy of the Keys collection see Hashtable.Keys.CopyTo.

Hope this helps
Jay

"Tony" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> I have this problem - I have a hashtable, containing a list of filenames.
>
> Every 60 seconds, I have a thread that enumerates thru this hashtable, and
> based on some simple logic, some of the items in the hashtable has to be
> removed.
>
> But when I remove a pair from the hashtable, inside the enumeration loop,

I
> get an exception.
>
> Why cant I remove from a hashtable, while enumerating ?
>
> My code looks something like this :
>
> for each r in camImages
> if r.value = x then
> camImages.remove(r.key)
> end if
> next
>
> When the above code is running, the first time the

"camImages.remove(r.key)"
> line is run, I get an exception in the last line, containing "next".
>
> Can someone explain to me what I am doing wrong ?
>
>
> -
> Regards,
> Tony Fonager
>
> Netcoders ApS
>
>



 
Reply With Quote
 
Tony
Guest
Posts: n/a
 
      24th Aug 2003
Jay,

Thanks for your reply and suggestions - I thought of doing it the same way
as you suggest, with a copy of the keys.

But before that, I just had to know if I was doing something wrong ;-)

-
Regards,
Tony Fonager

"Jay B. Harlow [MVP - Outlook]" <(E-Mail Removed)> wrote in message
news:#(E-Mail Removed)...
> Tony,
> > Why cant I remove from a hashtable, while enumerating ?

> Because the enumerator (an object that implements IEnumerator, an

interface
> used by For Each) may 'loose' track of where it is if you delete an item,
> the element the enumerator is on is no longer in the collection, so when

it
> goes to get the next element it has no idea where to look, as the internal
> structure of the HashTable has changed. Yea, Yea, The HashTable &

enumerator
> could let each other know when this happens, but the 'expense' of doing

this
> is greater then the 'benefit'.
>
> Generally the way to delete items while enumerating is to make a copy of

the
> Keys collection, then enumerator of this copy, or to add the items you

want
> deleted to a second collection (ArrayList for example). then delete based

on
> the items in this second collection. I tend to go for the second.
>
> To make a copy of the Keys collection see Hashtable.Keys.CopyTo.
>
> Hope this helps
> Jay



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
HashTable.Add Modifies HashTable Items? CoderHead Microsoft Dot NET Framework 8 14th Sep 2006 09:23 PM
Hashtable.Clone() is shadow copy, how to depth copy hashtable? harvie wang Microsoft C# .NET 6 29th Oct 2005 04:36 AM
SN.exe "Access is Denied" when attempting to generate a keypair =?Utf-8?B?cG1hcnNoYWxsMTk3MQ==?= Microsoft Dot NET 0 24th Jun 2005 05:06 PM
How to load my RSA keypair into the RSA class 小葉南洋杉 Microsoft VC .NET 0 31st Oct 2003 02:58 AM
copy Hashtable values to another Hashtable? M Microsoft C# .NET 3 17th Oct 2003 08:02 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:13 AM.