algorithmic modeling for Rhino
hi. im a noob trying to code in c#. Pardon my poor english.
I was trying to code something in c# and had a problem and was able to narrow it down to this so far.
It seems each time for statement loops, List x is also affected by instance.RemoveAt(i).
I could fix this problem by get x.Count value to int xc in the beginning of the code and use this, but shouldn't they output same data?
I do not understand why this occurs.
please see attached images too.
Tags:
List <int> instance = x;
This doesn't create a copy of the x list, it just creates a pointer to that data. It means that when you call instance.RemoveAt(i), you actually remove data from the x list. When you do that, the whole For Loop logic basically brakes. This is how you should create a copy of the list : http://stackoverflow.com/a/1952191
Thanks you so much I totally understand now!!
And if you wanna remove items while iterating through the loop, just go reverse:
for(int i = x.Count-1; i>=0; i--)
{
// do stuff
x.RemoveAt(i);
}
Welcome to
Grasshopper
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
© 2024 Created by Scott Davidson. Powered by