[Gambas-user] Variable reinitialized at each iteration of a loop structure

Brian G brian at westwoodsvcs.com
Wed Mar 29 01:17:27 CEST 2023


I Think I agree with the majority!

"Failure is the key to success; 
 each mistake teaches us something"  .. Morihei Ueshiba
Brian G

----- On Mar 28, 2023, at 3:12 PM, Christof Thalhofer chrisml at deganius.de wrote:

> Am 28.03.23 um 09:54 schrieb Benoit Minisini:
> 
>> By the way, I almost never declare variables in the middle of a
>> function. It's always a source of confusion, sometimes bugs, and make
>> the code less readable.
> 
> It was a bad idea, I was against that. But now I think, that a 'Dim'
> inside a function should behave like that to be logical:
> 
> If the code explicitly defines it's content it is clear that the
> variable contains the content defined:
> 
> -----------------------------------------------------------
> 
> For i = 0 to 100
>    Dim p as Integer = 0
>    Inc p
> Next
> 
> Print p
> 
> Prints 1
> 
> -----------------------------------------------------------
> 
> If the 'Dim' in the code does not explicitly define it's content, the
> preexisting content has to be preserved. This would be the same as if
> the variable were declared at the top of the method:
> 
> -----------------------------------------------------------
> 
> For i = 0 to 99
>    Dim p as Integer
>    Inc p
> Next
> 
> Print p
> 
> Prints 100
> 
> -----------------------------------------------------------
> 
> This is what Gambas does and IMO it should not be altered.
> 
> IMO it would be a bad idea and illogical if Gambas alters the content of
> the variable silently to '0' if the programmer did not explicitly wanted
> it. Because on the other hand if the programmer wants a change of the
> content, it must be defined in the code.
> 
> Alles Gute
> 
> Christof Thalhofer
> 
> --
> Dies ist keine Signatur
> 
> 
> 
> ----[ http://gambaswiki.org/wiki/doc/netiquette ]----


More information about the User mailing list