Enumerates constants.
[scope] Enum [Step <stepval>] <constantlist>
scope | [optional] The scope the Enum should be placed in, either Local, Global, Dim or none. If none, Dim behavior is used. |
stepval | [optional] The default step is to add 1. Other possible step methods are: *n, +n, -n where n is a whole number. |
constantlist | A list constants to be enumerated. |
By default, the first constant will be 0 and the rest will be incremented by 1 from there.
When using the multiply operator to step, the first constant will be assigned 1 and the rest will be multiplied based on the previous constant value.
Constants can be explicitly assigned by any valid statement.
Enums can be used as "easy-to-read" index values for an array - see Example 2 below.
#include <MsgBoxConstants.au3>
Example()
Func Example()
; These variables will enumerate from 0 to 2.
Local Enum $eVar1, $eVar2, $eVar3
MsgBox($MB_SYSTEMMODAL, "", "$eVar1 is equal to (0): " & $eVar1 & @CRLF & _
"$eVar2 is equal to (1): " & $eVar2 & @CRLF & _
"$eVar3 is equal to (2): " & $eVar3 & @CRLF)
; These variables will enumerate from 1 to 2 and then $eVariant3 is set to 5 which will continue to
; increment by 1.
Local Enum $eVariant1 = 1, $eVariant2, $eVariant3 = 5, $eVariant4
MsgBox($MB_SYSTEMMODAL, "", "$eVariant1 is equal to (1): " & $eVariant1 & @CRLF & _
"$eVariant2 is equal to (2): " & $eVariant2 & @CRLF & _
"$eVariant3 is equal to (5): " & $eVariant3 & @CRLF & _
"$eVariant3 is equal to (6): " & $eVariant4 & @CRLF)
; Multiply each enumeration by 2.
Local Enum Step *2 $eFoo1, $eFoo2, $eFoo3, $eFoo4
MsgBox($MB_SYSTEMMODAL, "", "$eFoo1 is equal to (1): " & $eFoo1 & @CRLF & _
"$eFoo2 is equal to (2): " & $eFoo2 & @CRLF & _
"$eFoo3 is equal to (4): " & $eFoo3 & @CRLF & _
"$eFoo3 is equal to (8): " & $eFoo4 & @CRLF)
EndFunc ;==>Example
#include <MsgBoxConstants.au3>
Example()
Func Example()
; Create variables in Local scope and enumerate through the variables. Default is to start from 0.
Local Enum $eCat, $eDog, $eMouse, $eHamster ; $eHamster is equal to the value 3, not 4.
; Create an array in Local scope with 4 elements.
Local $aAnimalNames[4]
; Assign each array element with the name of the respective animal. For example the name of the cat is Jasper.
$aAnimalNames[$eCat] = 'Jasper' ; $eCat is equal to 0, similar to using $aAnimalNames[0]
$aAnimalNames[$eDog] = 'Beethoven' ; $eDog is equal to 1, similar to using $aAnimalNames[1]
$aAnimalNames[$eMouse] = 'Pinky' ; $eMouse is equal to 2, similar to using $aAnimalNames[2]
$aAnimalNames[$eHamster] = 'Fidget' ; $eHamster is equal to 3, similar to using $aAnimalNames[3]
; Display the values of the array.
MsgBox($MB_SYSTEMMODAL, '', '$aAnimalNames[$eCat] = ' & $aAnimalNames[$eCat] & @CRLF & _
'$aAnimalNames[$eDog] = ' & $aAnimalNames[$eDog] & @CRLF & _
'$aAnimalNames[$eMouse] = ' & $aAnimalNames[$eMouse] & @CRLF & _
'$aAnimalNames[$eHamster] = ' & $aAnimalNames[$eHamster] & @CRLF)
; Sometimes using this approach for accessing an element is more practical than using a numerical value, due to the fact that changing the index value of
; the enum constant has no affect on it's position in the array. Therefore changing the location of $eCat in the array is as simple as changing the order
; it appears in the initial declaration e.g.
; Local Enum $eDog, $eMouse, $eCat, $eHamster
; Now $eCat is the 2nd element in the array. If you were using numerical values, you would have to manually change all references of $aAnimalNames[0] to
; $aAnimalNames[2], as well as for the other elements which have now shifted.
EndFunc ;==>Example