[Gambas-user] Loop variable question - lines of code in project

Benoît Minisini g4mba5 at gmail.com
Wed Feb 6 01:29:39 CET 2019


Le 06/02/2019 à 00:56, Richard Terry a écrit :
> 
> mm.... quite a bit of work to fix my loops, not to to mention the ones 
> in our libraries.
> 
> Richard
> 
> 
> ----[ Gambas mailing-list is hosted by https://www.hostsharing.net ]----
> 

It should be possible to automate the process:

1) The compiler gives you the name of the file and the line where the 
global variable is used as a loop variable.

2) Then you extract the name of the global variable from the line, that 
should look like: "<spaces>FOR EACH <variable> IN ..."

3) You find the line where the global variable is declared: "PRIVATE (or 
PUBLIC) <variable> AS <type>"

4) You extract the type of the global variable, and you rewrite the loop 
line that way: "<spaces>FOR EACH <variable> AS <type> IN ..."

5) You remove the global variable declaration.

That process won't work when the global variable is used for something 
else that loop variables, or if your code relies on side effects (for 
example if a method B uses the value of the loop global variable 
modified by the function A).

Anyway, it's a very bad programming pratice to use global variables as 
loop variables!

Regards,

-- 
Benoît Minisini


More information about the User mailing list