PC Review


Reply
Thread Tools Rate Thread

delete element of collection being iterated

 
 
Michal Januszczyk
Guest
Posts: n/a
 
      23rd Dec 2003
Hello.

Sometimes I encounter the following problem:

foreach( element of some collection)
{
delete this element;
}

of course, the runtime would not allow to modify
the collection whilst executing loop through
its elements.

I often happen to write solutoin to this problem
by myself, but i am curious if there is some
design pattern how to handle such a situation
(the need to delete element of collection being ierated
through)

Thaks for answer
Michal Januszczyk
 
Reply With Quote
 
 
 
 
Norman Yuan
Guest
Posts: n/a
 
      23rd Dec 2003
One trick is to use "for(){}" backward, insteadof "foreach(){}":

for (int i=myCollection.Count-1; i>=0; i--)
{
myCollection.RemoveAt(i)
//Or your own Delete(i)/Remove(i) method, because if you remove item
//from a collection backward, remaining item's index will not change
}

"Michal Januszczyk" <(E-Mail Removed)> wrote in message
news:036301c3c8ee$a22bb190$(E-Mail Removed)...
> Hello.
>
> Sometimes I encounter the following problem:
>
> foreach( element of some collection)
> {
> delete this element;
> }
>
> of course, the runtime would not allow to modify
> the collection whilst executing loop through
> its elements.
>
> I often happen to write solutoin to this problem
> by myself, but i am curious if there is some
> design pattern how to handle such a situation
> (the need to delete element of collection being ierated
> through)
>
> Thaks for answer
> Michal Januszczyk



 
Reply With Quote
 
Puneet Taneja
Guest
Posts: n/a
 
      23rd Dec 2003
Hi,

I also faced this problem and incorporated the following
solution for the same.

// Create a Hashtable and clone the existing HashTable
(accessible through the InnerHashtable property of the
types derived from the DictionaryBase class).

System.Collections.Hashtable LocalHashTable =
(System.Collections.Hashtable)this.InnerHashtable.Clone();

// Create an IEnumerator type instance, so as to traverse
the collection.

System.Collections.IEnumerator KeysEnumerator =
LocalHashTable.GetEnumerator();

// Finally traverse the collection
while (KeysEnumerator.MoveNext())
{
// Now you can manipulate the collection.
this.Dictionary.Remove(OldCellKey);
}



Regards,
Puneet Taneja


>-----Original Message-----
>One trick is to use "for(){}" backward,

insteadof "foreach(){}":
>
>for (int i=myCollection.Count-1; i>=0; i--)
>{
> myCollection.RemoveAt(i)
> //Or your own Delete(i)/Remove(i) method, because if

you remove item
> //from a collection backward, remaining item's index

will not change
>}
>
>"Michal Januszczyk"

<(E-Mail Removed)> wrote in message
>news:036301c3c8ee$a22bb190$(E-Mail Removed)...
>> Hello.
>>
>> Sometimes I encounter the following problem:
>>
>> foreach( element of some collection)
>> {
>> delete this element;
>> }
>>
>> of course, the runtime would not allow to modify
>> the collection whilst executing loop through
>> its elements.
>>
>> I often happen to write solutoin to this problem
>> by myself, but i am curious if there is some
>> design pattern how to handle such a situation
>> (the need to delete element of collection being ierated
>> through)
>>
>> Thaks for answer
>> Michal Januszczyk

>
>
>.
>

 
Reply With Quote
 
Rick Billings
Guest
Posts: n/a
 
      8th Sep 2006
If you create the iterator against a clone of the collection, you can delete from the original collection while continuing to iterate the clone

"
Private ie as IEnumerator = MyList.Clone.GetEnumerator()
"


From http://www.developmentnow.com/g/34_2...ng-iterated.ht

Posted via DevelopmentNow.com Group
http://www.developmentnow.com
 
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
delete element is collection Gary Keramidas Microsoft Excel Programming 3 14th Nov 2007 10:48 PM
Win32_OperatingSystem collection cannot be iterated Andrew Aronoff Windows XP WMI 1 21st Apr 2006 03:05 PM
configuration element and collection Andrzej Kaczmarczyk Microsoft C# .NET 1 12th Dec 2005 03:49 PM
collection doesn't find element =?Utf-8?B?c29yaW5jb20=?= Microsoft Dot NET 1 11th Aug 2005 03:31 PM
Swap element in Controls Collection? Perlith Microsoft Access Reports 0 29th Oct 2003 10:51 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:27 PM.