Enums are essentially a thin layer over a primitive numeric type (by default Int32
). Internally they are handled exactly like numeric values and they can be cast to and from their underlying type readily.
Because of this, it's worth noting that when dealing with Enums you have to be careful around handling the values you are passed.
Enum eOpenMode
Add = 1
Edit = 2
End Enum
Dim myEnumVal as eOpenMode
myEnumVal = Cast(100, eOpenMode)
This code compiles and runs fine and myEnumVal
will contain a value of 100, which is not mapped to a known enumerated value. It would then be legal for me to pass this value to a method expecting an eOpenMode
argument, even though it is not one of the enumerated values.
Be sure to check enum values using Switch
statements and throw ArgumentOutOfRangeException
if the value supplied is not defined in the enumeration.
In addition to this, because enums are backed by numeric types, they can also be utilized as bit-masks to allow a combination of values:
<Flags>
Enum eOpenMode
Add = 1
Edit = 2
AddEdit = Add OR Edit
End Enum