473,327 Members | 2,007 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,327 software developers and data experts.

Iterating a collection and removing item


How can I iterate a collection and conditionally remove items from the
collection using either C# or VB.NET?

When I try to remove an item, I get the error message that I am not allowed
to remove an item.

Thanks
Jul 21 '05 #1
4 2011
Instead of using a For Each (foreach in C#) loop, iterate over the
collection using an index and *make sure* to start from the bottom up.

For i As Integer = myCollection.Count - 1 To 0
If something Then
myCollection.Remove(i)
End If
Next i

for(int i = myCollection.Count - 1; i>=0; i--)
{
If(something)
{
myCollection.RemoveAt(i);
}
}

I'm sure you see why I stressed on starting from the bottom :)

hope that helps..
Imran.

"STech" <ST***@discussions.microsoft.com> wrote in message
news:05**********************************@microsof t.com...

How can I iterate a collection and conditionally remove items from the
collection using either C# or VB.NET?

When I try to remove an item, I get the error message that I am not
allowed
to remove an item.

Thanks

Jul 21 '05 #2
On Wed, 13 Oct 2004 23:06:01 -0400, Imran Koradia wrote:
Instead of using a For Each (foreach in C#) loop, iterate over the
collection using an index and *make sure* to start from the bottom up.

For i As Integer = myCollection.Count - 1 To 0


Don't you need a Step -1 on the end of this line?

--
Chris

dunawayc[AT]sbcglobal_lunchmeat_[DOT]net

To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
replace certain words in my E-Mail address.
Jul 21 '05 #3
>> For i As Integer = myCollection.Count - 1 To 0

Don't you need a Step -1 on the end of this line?


duh! thats right. my apologies :(
Jul 21 '05 #4
This is currently how I am doing it (reverse loop) . I was hoping that there
was a different method to do it.

Thanks.

"Imran Koradia" wrote:
For i As Integer = myCollection.Count - 1 To 0


Don't you need a Step -1 on the end of this line?


duh! thats right. my apologies :(

Jul 21 '05 #5

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

Similar topics

3
by: Patrick von Harsdorf | last post by:
I want to iterate over a collection and delete all unwanted entries. for item in collection: del item of course doesn´t do anything useful. Two things come to mind: a) iterating backwards...
4
by: Michael | last post by:
Dear all .. If I want to use develop a user control and declare a public property which the type is System.Windows.Forms.GridTableStylesCollection For example : Public Class LookAndView...
4
by: STech | last post by:
How can I iterate a collection and conditionally remove items from the collection using either C# or VB.NET? When I try to remove an item, I get the error message that I am not allowed to remove...
6
by: Ken Fine | last post by:
I'm using SQLDataSource, which generates some kind of dataset, and then I attach that datasource to various data display controls such as DataList and repeater which loop through to the end of the...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.