枚举
最后更新于:2022-04-02 04:15:49
[TOC]
## 用枚举提高复杂性
bad
`if chosenColor=1`
good
`if chosenColor = Color_Red`
用于函数参数
bad
`int result = RetrievePayRollData(data,true,false,false,true)`
good
```
int result = RetrievePayrollData(
data,
EmploymentStatus_CurrentEmployee,
PayRollType_salaried,
SavingsPlan_NoDeduction,
MedicalCoverage_IncludeDependents
)
```
## 定义枚举的第一项和最后一项,以便于循环边界
```
Public Enum Country
Country_First= 0
Country_China= 0
Country_England= 1
Country_France= 2
Country_Germany a 3
Country_India= 4
Country_Japan=5
Country_Usa= 6
Country_Last = 6
End Enum
```
## 把枚举类型的第一个元素做非法值
把映射到0的那个元素声明为无效会有助于捕捉那些没有合理初始化的变量
```
Public Enum Country
Country_InvalidFirst= 0
Country_China= 0
Country_England= 1
Country_France= 2
Country_Germany a 3
Country_India= 4
Country_Japan=5
Country_Usa= 6
Country_Last = 6
End Enum
```
';