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

Christof Thalhofer chrisml at deganius.de
Wed Mar 29 00:12:47 CEST 2023


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

-------------- next part --------------
A non-text attachment was scrubbed...
Name: OpenPGP_signature
Type: application/pgp-signature
Size: 840 bytes
Desc: OpenPGP digital signature
URL: <http://lists.gambas-basic.org/pipermail/user/attachments/20230329/bc28eadf/attachment-0001.sig>


More information about the User mailing list