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

Removing an object from an Enumerated Collection

Hi,
I need to remove an object from a collection that has been enumerated
with GetEnumerator similar to this...

With lobjs.GetEnumerator
.MoveNext()
if [some_condition] Then
lobjs.remove(.Current)
end if
end with

I realise you can't use the Remove with the GetEnumerator and bombs on
the next MoveNext. is there another way to do this? The API I'm
using requires GetEnumerator (as opposed to For...Each).

Cheers!
Jun 27 '08 #1
3 1735
The question has to be - why?

Is the world going to stop turning if iterate over the collection with For
Each ... Next, or even, for that matter, For ... Next?

I can't imagine any valid reason for the API 'requiring' you to use an
Enumerator.

I can, however, imagine an 'in-house' policy that requires the use of a
Enumerator, but if that is the case then say so, rather than making some
vague statement that doesn't make any sense.

If you remove an item from a collection while you are iterating over it,
then, the rule of thumb is that the collection becomes changed and iterator
is rendered invalid.

This is regardless of whether you are using an Enumerator, a For Each ...
Next iterator or a For ... Next iterator.

If you use a 'backwards' For ... Next iterator then you can remove items
quite happily:

For _i = lobjs.Count - 1 To 0 Step -1
If condition Then lobjs.RemoveAt(_i)
Next

In your case, you could use another collection and do a 2 stage update:

Dim _remove = New List(Of Object)

With lobjs.GetEnumerator
.MoveNext()
If condition Then _remove.Add(.Current)
End With

For Each _obj In _remove
lobjs.Remove(_obj)
Next
"gee-dub" <up*****@shaw.cawrote in message
news:21**********************************@l42g2000 hsc.googlegroups.com...
Hi,
I need to remove an object from a collection that has been enumerated
with GetEnumerator similar to this...

With lobjs.GetEnumerator
.MoveNext()
if [some_condition] Then
lobjs.remove(.Current)
end if
end with

I realise you can't use the Remove with the GetEnumerator and bombs on
the next MoveNext. is there another way to do this? The API I'm
using requires GetEnumerator (as opposed to For...Each).

Cheers!
Jun 27 '08 #2
gee-dub wrote:
Hi,
I need to remove an object from a collection that has been enumerated
with GetEnumerator similar to this...

With lobjs.GetEnumerator
.MoveNext()
if [some_condition] Then
lobjs.remove(.Current)
end if
end with

I realise you can't use the Remove with the GetEnumerator and bombs on
the next MoveNext. is there another way to do this? The API I'm
using requires GetEnumerator (as opposed to For...Each).

Cheers!
The For Each uses an enumerator, so unless the enumerator is crap, there
is no problem in using a For Each.

As Stephany suggested, to remove the items you just put them in a new
list, and remove them in a separate loop.

--
Göran Andersson
_____
http://www.guffa.com
Jun 27 '08 #3
gee-dub wrote:
I need to remove an object from a collection that has been enumerated
with GetEnumerator similar to this...
As you've discovered, you can't.
Removing an item invalidates the enumerator on those items.
I realise you can't use the Remove with the GetEnumerator and bombs on
the next MoveNext. is there another way to do this?
Yes. Let the enumerator do its job and, if you want to get rid of an
item, add it to another list so that you can purge them out once you the
"the end", something like:

e = data.GetEnumerator()

Do While e.MoveNext()
if NeedToDelete( e.Current ) then
trash.add( e.Current )
end if
Loop

' Remove the ones we've marked for deletion
' (backwards, of course)
For i As Integer = data.Count - 1 To 0 Step -1
If trash.Contains( data( i ) ) then
data.RemoveAt( i )
End If
Next
The API I'm using requires GetEnumerator
(as opposed to For...Each).
Never come across anything like that before.
Which one is it?

Regards,
Phill W.
Jun 27 '08 #4

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

Similar topics

0
by: TP-Software | last post by:
Hi, I have a ChatFormCollection class which holds a collection of the class ChatForm which inherits from Form. MainForm has an instance of ChatFormCollection. When a ChatForm instance is...
1
by: Kevin in Chicago | last post by:
Hello all, I have written the following code that both enumerates and also removes from the collection being enumerated. As the code illustrates, this causes me to have to reset the...
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...
1
by: Mike in Paradise | last post by:
Is there a more effcient way of removing the spaces from the names for a Enumerated value that has several values when you split it)??? When you do a toString it puts ,<SPACE> between the entries...
4
by: Alexandre Soares | last post by:
Hi, On some pages, I put a collection in the session, which uses the InProc mode. On pages where the session is extensively used (many collections are put into it), whenever I try to remove an...
0
by: Adam J. Schaff | last post by:
Hello. I have a custom collection that implements IBindingList (allownew and allowremove are both true). I have bound it to a datagrid. I have add and remove buttons on the screen. I want to...
6
by: Niyazi | last post by:
Hi all, What is fastest way removing duplicated value from string array using vb.net? Here is what currently I am doing but the the array contains over 16000 items. And it just do it in 10 or...
1
by: senfo | last post by:
I'm using an enumerated type to identify a record type that relates to a unique ID in a database. Identity columns in SQL start at 1, while enumerated types in C# start at 0. I didn't think it...
0
by: Mike | last post by:
Hi, I have a collection object bound to a data grid, after I remove an item from the collection, the minute I click on the datagrid I get an error saying the specified argument was out of the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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,...
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,...
0
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...

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.