Jump to content

Recommended Posts

Posted (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 by Zog
learned how to insert code snippets. Change to this for reading clarity
  • Developers
Posted

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. 

 

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.
  :)

Posted (edited)

Simple errors from simple minds!!  

That works fine - thank you!

Edited by Zog
fix typo
Posted (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 by MattyD

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...