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

RemoveAt() breaks for loop

Markus
6,050 Expert 4TB
Ok, so after some research I found that you have to iterate backwards through an array if you're removing elements. But the reason as to why has eluded me.

Can someone explain why this doesn't work and always deletes 1 less than the amount I have selected:

Expand|Select|Wrap|Line Numbers
  1. for (int i = 0; i < IPArray.Count; ++i)
  2.             {
  3.                 if (dataGridView1.Rows[i].Cells[2].Value != null)
  4.                     if ((bool)dataGridView1.Rows[i].Cells[2].Value == true)
  5.                     {
  6.                         MessageBox.Show(dataGridView1.Rows[i].Cells[2].Value.ToString());
  7.                         IPArray.RemoveAt(i);
  8.                     }
  9.             }
  10.  
While this works perfectly:

Expand|Select|Wrap|Line Numbers
  1. for (int i = IPArray.Count - 1; i >= 0; --i)
  2.             {
  3.                 if (dataGridView1.Rows[i].Cells[2].Value != null)
  4.                     if ((bool)dataGridView1.Rows[i].Cells[2].Value == true)
  5.                     {
  6.                         MessageBox.Show(dataGridView1.Rows[i].Cells[2].Value.ToString());
  7.                         IPArray.RemoveAt(i);
  8.                     }
  9.             }
Cheers.
Nov 7 '08 #1
6 2266
joedeene
583 512MB
Try using the following line of code in your first line of your first code:

Expand|Select|Wrap|Line Numbers
  1. for (int i = 0; i <= IPArray.Count; ++i)
Notice you just had the less than sign(<) which means whatever the selected items are it has to be less. So, if you selected 8, it can't be 8 because that's equal to 8. But 7 is equal to 8.

So I just replaced the less than sign with a less than OR equal to sign(<=). This should work since you said it was working for all selected minus one. Does this help?

joedeene
Nov 7 '08 #2
Markus
6,050 Expert 4TB
No, that doesn't solve it. It's something to do with the way the elements are removed from the array, I think.

Anyway, got to run.
Nov 7 '08 #3
Plater
7,872 Expert 4TB
Well your loop condition is based on the number of items in the array.
If there are 10 items in the array lets say, your loop will go from index 0 to 9 (correctly)
If you remove the item at index [2], now your loop will only go from 0 to 8.
...Well now I don't know where I was going with this.
BUT, if you tried to remove an item while using a foreach loop, it throws exceptions (due to changes in itterators or something)

What you could do is mark the indexes that you want to delete somewhere, and then loop through THAT collection to delete the indexes in a different collection.
Nov 7 '08 #4
Curtis Rutland
3,256 Expert 2GB
No, no, Plater, you were on the right track. If you're looping through an array, and deleting elements, you need to decriment your counter variable when you remove an entry.

So add an i--; after you remove an element.
Nov 7 '08 #5
tlhintoq
3,525 Expert 2GB
Picture the array as a vertical stack of Jenga game pieces, with 0 being the bottom piece, and 10 being the top piece.

If you increment from 0 to 10, moving item zero...
Item 1 'drops down' to fill the void where item zero used to be, and your stack is now 9 items tall.

But then in your loop, you are incrementing your counter, so now you remove item 1, forgetting that you still have a new item zero that used to be item 1.

Then increment to item 2... and so on,

So you remove...
Item 0 from an array of 10
Item 1 from an array of 9
Item 2 from an array of 8
Item 3 from an array of 7
Item 4 from an array of 6
Item 5 from an array of 5 - Breaks here since it is zero indexed

If you are going to increment your counter, then you would have to go from zero to 10, always removing item zero, not item "indexed counter variable"
Nov 8 '08 #6
Plater
7,872 Expert 4TB
But then in your loop, you are incrementing your counter, so now you remove item 1, forgetting that you still have a new item zero that used to be item 1.
That's the part I was missing. I *knew* I had learned it before but couldn't remember.
Nov 10 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: alanrn | last post by:
I've implemented a number of strongly-typed collections that inherit from CollectionBase and recently noticed something that I don't fully understand. CollectionBase defines method RemoveAt(). ...
5
by: Eric Culus | last post by:
I seem to be having troubles with this page.... It's an automated mailing script... Steps: - system checks on x dates the variables linked to a maintenance contract (query on db) - when...
1
by: Glenn M | last post by:
in the following code For Each child In nodeFarm.ChildNodes For Each oAttribute In child.Attributes If oAttribute.Name = "LastUpdated" Then lastUpdate = oAttribute End If Next If...
7
by: Alex Ting | last post by:
Hi Everybody, I have an issue about deleting an object from an arrayList. I've bounded a datagrid using this code where it will first run through all of the code in loadQuestions() and bind a...
0
by: Hasani | last post by:
When I have a form, and I want to remove Controls from the form, I call Form.Control.RemoveAt. When I make this call, do I have to manually dispose of the object? I think not, but I just want to...
2
by: Mike | last post by:
I need my textbox to work more smoothly with respect to line breaks. When I have data pulled from the database into a textbox there are hard line breaks at the end of each line (by definition how...
2
by: George | last post by:
Hi, I have encountered the following problem. Say I have the following, DataGridView dgv; DataTable dt; OleDbDataAdapter oleDb_da; (bound to a OleDb data connection)
17
by: Hyun-jik Bae | last post by:
Is there any efficient way doing Dictionary<>.RemoveAt in foreach { ... } routine? For example, is this example safe or is there any recommended way? foreach(KeyValuePair<int,inta in mymap) {...
0
by: =?Utf-8?B?R3JleWhvdW5k?= | last post by:
I have a DataGridView and I need to allow users to select multiple rows and press a delete button. If the user has the last row selected, the all the data in the grid is deleted. If they choose...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...
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,...

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.