GLBasic forum

Main forum => Bug Reports => Topic started by: MrTAToad on 2011-Mar-06

Title: Interesting variable strangeness
Post by: MrTAToad on 2011-Mar-06
As you can see in the following code :

Code (glbasic) Select
FUNCTION buildClientListToSend$:toAddName$="",toAddIP%=0
LOCAL temp$

temp$=""
FOREACH loop IN self.playerList[]
INC temp$,loop.name$+SEPERATOR$+loop.ipAddress%+SEPERATOR$+loop.isHost%+SEPERATOR$+loop.isReady%+SEPERATOR$
NEXT

IF toAddName$<>"" THEN INC temp$,toAddName$+SEPERATOR$+toAddIP%+SEPERATOR$+FALSE+SEPERATOR$+FALSE+SEPERATOR$
DEBUG temp$+"\n"

RETURN LEFT$(temp$,LEN(temp$)-1)
ENDFUNCTION


The variable loop isn't defined, nor is it a global variable - and I have explicit declarations on.  The only reason I think the compiler compiles the program is that loop is defined in another function...
Title: Re: Interesting variable strangeness
Post by: kanonet on 2011-Mar-06
No, its a normal behavior. Foreach defines a local variable (for das it the same way) that existis only in the for-next loop, you dont have to define it before. At least, i think its intended, 'cuz it works this way for a long time.
Title: Re: Interesting variable strangeness
Post by: MrTAToad on 2011-Mar-07
Yes, that does sound familiar...  In which case its not strange :)
Title: Re: Interesting variable strangeness
Post by: Moebius on 2011-Mar-07
You can check in the temp files to see what's actually happening - temporary local variable is used to store the index, and the actual 'variable' you access is a temporary, localled reference.  It makes sense  8)  This also means I've been creating a lot of pointless variables all this time...
Title: Re: Interesting variable strangeness
Post by: MrTAToad on 2011-Mar-07
Always better to be safe though :)