This page shows how to declare and use enumerated constants. Using enumerated constants, you can more easily add new values, and share the named constants with any clients of your component.
To declare enumerated constants:
This code defines 4 constants to indicate the state of a weapon. The first value is set to 500. Each subsequent value is incremented by one.
Public Enum enumWeaponStates
gunLoaded = 500
gunEmpty
gunJammed
gunDamaged
End Enum
To reference an enumerated constant:
This code checks to see if the gun is loaded before firing.
If objPistol.State = gunLoaded Then
Call FireGun(objPistol)
Else
Call ReloadGun(objPistol)
End If
Validating parameters with enumerated constants:
If a value is passed to a method or procedure which is to be compared with an enumerated constant, it is possible to have the value automatically validated as being in an acceptable range by specifying the parameter with the enumerated data type, as follows:
Private Sub RepairWeapon(ByRef intWeaponState As enumWeaponState) ... End Sub
If a value is passed in intWeaponState which does not appear in the enumerated constants, then the calling procedure will have an error raised, which can be trapped.