Zog Posted January 5 Posted January 5 (edited) Question: is there any way to have '#include' files have reference to Global variables without explicitly including the Global variable in the calling function? Here is a short example showing the issue: "Foo.au3" which is: #include-once Func Foo() ConsoleWrite("$debug="&$debug&@CRLF) EndFunc "Foo2.au3" which is: #include <Foo.au3> Global $debug = True Foo() Exit When executing Foo2 I get this error: Foo.au3"(2,30) : warning: $debug: possibly used before declaration. ConsoleWrite("$debug="&$debug& ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^ Now, lets assume I have a file called "Foo3.au3" which is: Global $debug = True Foo() Func Foo() ConsoleWrite("$debug="&$debug&@CRLF) EndFunc When I run Foo3 it works as expected. Foo3 is just, in my mind, the Foo2 with Foo included. What am I missing please? Edited January 5 by Zog learned how to insert code snippets. Change to this for reading clarity
Developers Solution Jos Posted January 5 Developers Solution Posted January 5 It simply comes down to putting the lines in the right order. Just imagine the included file being part of its master at the point of the include statement. So put the global statement first and it will be known in your include file. Festerini 1 SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past.
Zog Posted January 5 Author Posted January 5 (edited) Simple errors from simple minds!! That works fine - thank you! Edited January 5 by Zog fix typo
MattyD Posted January 6 Posted January 6 (edited) just to add my 2 cents.. In most cases you'd declare that $debug switch within the dependency. That's because Foo() needs it, and that's where the func lives. #include-once Global $debug = False ;Default value Func Foo() ConsoleWrite("$debug="&$debug&@CRLF) EndFunc And just switch it on/off in your main script as needed. #include <Foo.au3> $debug = True Foo() Exit Edit: Or if you're getting a bit fancier.. This probably makes your include file behave a bit more like a drop-in module. #include-once Global $__g_bDebug = False ;Default value Func Foo() ConsoleWrite("$debug="&$__g_bDebug&@CRLF) EndFunc Func SetDebug($bState) $__g_bDebug = ($bState = True) ;Forces $__g_bDebug to be a boolean type (True/False) EndFunc Main script #include <Foo.au3> SetDebug(True) Foo() Edited January 6 by MattyD
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now