AutoIt (Variables)

From The Ultimate Programming Reference

(Difference between revisions)

Current revision as of 11:35, 26 March 2006

[edit] Variables

A variable is just a place to store data in memory so that it can be accessed quickly. Think of it as a mailbox in memory that you can put information in or take information out of. For example you might create a variable to store the number a user's response to a question, or the result to a math equation.

Each variable has a name (again, similar to a mailbox) and must start with the $ character and may only contain letters, numbers and the underscore _ character. Here are some example names:

   $var1
   $my_variable


Each variable is stored as a variant.


[edit] Declaring Variables

Variables are declared and created with the Dim, Local and Global keywords:

   Dim $var1

Or you can declare multiple variables at once:

   Dim $var1, $myvariable


You can also assign a variable without declaring it first, but many prefer explicit declarations.

   $var1 = "create and assign"


[edit] Scope

A variable's scope is controlled by when and how you declare the variable. If you declare a variable at the start of your script and outside any functions it exists in the Global scope and can be read or changed from anywhere in the script.

If you declare a variable inside a function it is in Local scope and can only be used within that same function. Variables created inside functions are automatically destroyed when the function ends.

By default when variables are declared using Dim or assigned in a function they have Local scope unless there is a global variable of the same name (in which case the global variable is reused). This can be altered by using the Local and Global keywords to declare variables and force the scope you want.

Personal tools