How to write into an enumerator

  • Thread starter Thread starter ~toki
  • Start date Start date
T

~toki

Having this,
// pseudocode
{
// Fill the whole SystemArray
t.position = 500;
}

// *Code*
for (int i = 0 ; i < 1000; i ++)
{
System.Collections.IEnumerator enume = trees.GetEnumerator();
while( enume.MoveNext() )
{
Tree t = (Tree)enume.Current;
t.position.X++;
}
}

With this code t.position are always 500 :(
How can i do to work t.postion++;
Is possible to write *into* the enumerator
 
toki,

Generally speaking, it is not advisable to modify the enumeration itself
while enumerating through it. Modifying values on objects/structures
returned from the enumeration is fine, but modifying the actual list itself
is a bad thing.

That being said, the cause of your problem is because of the fact that
the position field/property on your Tree object is a structure. When the
position property returns its value, it is actually returning a copy, and
then the X property on that structure is incremented, not the original.
What you have to do is:

Tree t = (Tree)enume.Current;

// I'm assuming it is a point structure.
Point p = t.position;
p.X = p.X + 1;

// Set position back.
t.position = p;

Hope this helps.
 
Back
Top