Declare a variable, a constant, or create an array.
Global | Local [Const] $variable [ = initializer ]
Global | Local [Const] $aArray[subscript 1]...[subscript n] [ = initializer ]
| Const | [optional] If present, the Const keyword creates a constant rather than a variable. |
| $variable | The name of the variable/constant to declare. |
| initializer | The value that will be initially assigned to the variable. A Const must include the initializer. The initializer can be a function call. |
| subscript | The number of elements to create for the array dimension, indexed 0 to n-1. |
The Dim/Local/Global keywords perform similar functions:
1. Declare a variable before you use it (similar to VBScript)
2. Create an array
Note: In AutoIt you can create a variable simply by assigning a value ($myvar = 0) but many people like to explicitly declare them. If AutoItSetOption("MustDeclareVars", 1) is active, then variables must be declared prior to use.
You can also declare multiple variables on a single line:
Local $vVar_1, $vVar_2, $vVar_3
Local $vVar_1 = 10, $vVar_2 = "20", $vVar_3 = 30
Const $CONST_1 = 1, $CONST_2 = 2, $CONST_3 = 3
Global Const $PI = 3.14, $MEANING_OF_LIFE = 42
Local Const $iApples = 500
Local $aArray_1[12] = [3, 7.5, "string"], $aArray_1[5] = [8, 4, 5, 9, 1]
Local $aGrid[2][4] = [["Paul", "Jim", "Richard", "Louis"], [485.44, 160.68, 275.16, 320.00]]
Global $aTest[5] = [3, 1, StringSplit("Abe|Jack|Bobby|Marty", "|"), Cos(0)]
AutoItSetOption, UBound, ReDim, Static
#include <Array.au3>
; Declaring variables
Local $i, $j = 23, $k
Global $g_fPI = 3.14159, $g_iRADIUS
Local $iDaysWorking = 5
; Declaring arrays
Global $g_aChessBoard[8][8] ; 2 dimensions, 8 rows, 8 colums - empty array elements
Global $g_aEmptyArray[0] ; 1 dimension, 0 rows - empty array elements
Global $g_aAutoSize[] = [1, 2, 3, 4] ; 1 dimension auto determined size - filled with 4 rows (array elements)
Local $aStates[2], $aWindowsStats[4] ; 1 dimension, 2 rows and 1 dimension, 4 rows - both empty array
Local $a2Dimensions[2][3] = [[1, 2, 3], [4, 5, 6]] ; 2 dimensions, 2 rows, 3 colums - filled array elements
; Declaring constant variables
Const $iX1 = 11, $iY1 = 23, $iZ1 = 55
Global Const $PI = 3.14159, $E = 2.71828
Local Const $DAYS_WORKING = 5
; checking $a2Dimensions "properties"
ConsoleWrite('Dimension=' & UBound($a2Dimensions, $UBOUND_DIMENSIONS) & @CRLF)
ConsoleWrite('Rows=' & UBound($a2Dimensions, $UBOUND_ROWS) & @CRLF)
ConsoleWrite('Columns=' & UBound($a2Dimensions, $UBOUND_COLUMNS) & @CRLF)
; get/check array values
ConsoleWrite('Value from 1D array=' & $g_aAutoSize[0] & @CRLF) ; value from [first row] of 1D array
ConsoleWrite('Value from 2D array=' & $a2Dimensions[0][0] & @CRLF) ; value from [first row][first column] of 2D array
#AutoIt3Wrapper_Au3Check_Parameters=-q -d -w- 3 -w- 6 ; Already declared var=off, warn when using Dim=off
#include <MsgBoxConstants.au3>
Dim $vVariableThatIsGlobal = "This is a variable that has ""Program Scope"" aka Global."
MsgBox($MB_SYSTEMMODAL, "", "An example of why Dim can cause more problems than solve them.")
Example()
Func Example()
; That looks alright to me as it displays the following text: This is a variable that has "Program Scope" aka Global.
MsgBox($MB_SYSTEMMODAL, "", $vVariableThatIsGlobal)
; Call some random function.
Local $vReturn = SomeFunc()
; The Global variable ($vVariableThatIsGlobal) changed because I totally forgot I had a duplicate variable name in "SomeFunc".
MsgBox($MB_SYSTEMMODAL, $vReturn, "The variable has now changed: " & $vVariableThatIsGlobal)
EndFunc ;==>Example
Func SomeFunc()
; This should create a variable in Local scope if the variable name doesn't already exist.
; For argument sake I totally forgot that I declared a variable already with the same name.
; Well I only want this to be changed in the function and not the variable at the top of the script.
; Should be OK right? Think again.
Dim $vVariableThatIsGlobal = ""
For $i = 1 To 10
$vVariableThatIsGlobal &= $i ; This will return 12345678910 totally wiping the previous contents of $vVariableThatIsGlobal.
Next
Return $vVariableThatIsGlobal
EndFunc ;==>SomeFunc