Zog Posted Monday at 06:01 PM Posted Monday at 06:01 PM (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 Monday at 07:05 PM by Zog learned how to insert code snippets. Change to this for reading clarity
Developers Jos Posted Monday at 06:09 PM Developers Posted Monday at 06:09 PM 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 Monday at 06:48 PM Author Posted Monday at 06:48 PM (edited) Simple errors from simple minds!! That works fine - thank you! Edited Monday at 07:06 PM by Zog fix typo
MattyD Posted yesterday at 10:56 AM Posted yesterday at 10:56 AM (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 yesterday at 11:08 AM 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