Jump to content
guner7

Function able to access Local variable

Recommended Posts

Hi,

Below is the code I've wrote long ago and ran it very frequent without any issue.

I am doing some clean up recently and revisit the code just to find out that the ReplaceTheApp() function is able to access the variable I declared in Local. How can this possible while I didn't pass any variable into the function or declare the variable in Global?

Local $UpdateInfo = IniReadSection(@AppDataDir & "\AutoUpdater.ini", "Update")
Local $WorkingPath = $UpdateInfo[1][1]
Local $AppName = $UpdateInfo[2][1]
Local $WindowsTitle = $UpdateInfo[3][1]
Local $Repository = $UpdateInfo[4][1]

ReplaceTheApp()
Run($WorkingPath & "\" & $AppName)

Func ReplaceTheApp()
    SplashTextOn("Updating..", "Downloading new updates for installation, please wait..", 400, 60, -1, -1)
    FileDelete($WorkingPath & "\" & $AppName)
    InetGet("https://something.com/" & $Repository & "/download/" & $AppName, $WorkingPath & "\" & $AppName)
    Sleep(1000)
    SplashOff()
EndFunc

Thank you.

Share this post


Link to post
Share on other sites
11 hours ago, guner7 said:

How can this possible while I didn't pass any variable into the function or declare the variable in Global?

When you declare a variable in the global scope of a script (not in a function), then it is always Global, even if you use the keyword Local !

Edit : @guner7  

There is only one global scope for all (script, includes), see example below :

Main script (save as main.au3):

; Mainscript :
Global $g_sText = 'This is a text'

ConsoleWrite('@@ 1. $g_sText = ' & $g_sText & @CRLF)

ConsoleWrite('! before #include -> IsDeclared("g_sIncludeGlobal") = ' & IsDeclared("g_sIncludeGlobal") & @CRLF)
ConsoleWrite('< --> #include "Includescript.au3'  & @CRLF)
#include "Includescript.au3"
ConsoleWrite('< --> #include Done'  & @CRLF)
ConsoleWrite('+ after #include -> IsDeclared("g_sIncludeGlobal") = ' & IsDeclared("g_sIncludeGlobal") & @CRLF)

ConsoleWrite('@@ 3. $g_sText = ' & $g_sText & @CRLF)

Include script (save as Includescript.au3) :

; Includescript :
Global $g_sIncludeGlobal = 'some other text'
ConsoleWrite('@@ 2. $g_sText       = ' & $g_sText & @CRLF)
ConsoleWrite('@@ $g_sIncludeGlobal = ' & $g_sIncludeGlobal & @CRLF)

 

Edited by Musashi
enhancement

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...