On 27 Nov 2009, at 15:49, Juha Manninen wrote:

If I define a private integer:

 TDemo = class(TCustomApplication)
 private
   i: integer;
   ...

and in a protected method I use it:
 for i := 0 to List.Count - 1 do
   ...

then I get an error:
 Error: Illegal counter variable

Moving it to method's var section helps.
Method's var section is also better place for it in general, but still it
should be legal to use a private member as counter var.

The reason is that a for-loop tries to make a number of reasonable guarantees that the counter variable cannot be modified during the loop. E.g., direct assignments to a counter variable are forbidden inside the loop (except in TP-mode). If you use a private field, then any method of that class called in the body of the loop can easily (accidentally) change the value of the counter variable, completely messing up the loop.


Jonas
_______________________________________________
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply via email to