Jump to content
Sign in to follow this  
JunkMaster

Undeclared variables don't cause an error

Recommended Posts

JunkMaster

I am just starting to write my first script in AutoIt. I was looking through the examples in the helpfile and found the example for _GUICtrlTreeView_ExpandedOnce.au3. In this example they use $x and $y, but do not declare them. The script includes the directive:

Opt('MustDeclareVars',1).

My understanding is this means all variables must be defined before they are used. I ran a quick test and found that:

Opt('MustDeclareVars', 1)
$A = 1
msgbox(1,"Test $A", "$A = " & $A)

generates a "Variable used without being declared." error. But this does NOT generate an error:

Opt('MustDeclareVars', 1)
for $A = 1 to 4
    msgbox(1,"Test $A", "$A = " & $A)
Next

What am I missing?

Share this post


Link to post
Share on other sites
Rawox

I think it is because of the fact that $A = 1 is actually declaring the $A variable...

Share this post


Link to post
Share on other sites
Tvern

Your first example won't error if you use "Local $A = 1", but I think you realise that.

Ftr the second example read the helpfile on Keyword For...To...Step...Next:

The Variable will be created automatically with a LOCAL scope, even when MustDeclareVars is on.

Share this post


Link to post
Share on other sites
JunkMaster

Your first example won't error if you use "Local $A = 1", but I think you realise that.

Ftr the second example read the helpfile on Keyword For...To...Step...Next:

Thank's Tvern. That is the missing info. I never thought to look at the definition of the For loop.

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
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.