Enum
枚举常量.
[作用域] Enum [Step <步长值>] <常量列表>
参数
作用域
|
[可选] 枚举的作用范围: Local, Global, Dim 或不指定. 如果不指定, 则使用 Dim.
|
步长值
|
[可选] 默认为每次添加1. 其它可能的步进方法: *n、+n、-n, n 为整数.
|
常量列表
|
枚举常量的列表.
|
备注
默认第一个常量为 0, 其后将增量 1 或按指定步进值操作.
当使用步进倍乘运算时, 第一个常量将被赋值 1, 而其余的将倍乘前一个常量值.
常量能被任何有效的语句赋值.
函数示例
示例 1
#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
示例 2
#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
----------------------------------------
|