Jump to content

Undeclared variables don't cause an error


Recommended Posts

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...