A Enum is simply a type-safe constant.
Public Enum CardSuit Spades Clubs Diamonds Hearts End Enum
Public Enum CardFace Two Three Four Five Six Seven Eight Nine Ten Jack Queen King Ace End Enum
Let's define a simple structure that utilises the two enums (CardSuit & CardFace).
The good thing about enums it makes understand your easier.
Dim Card_A = PlayingCard.CreateNew(CardFace.Ace, CardSuit.Spades)
You can easily till I'm a creating a new instance of the Playing Card Structure with the value Ace Of Spades.
This is valid.
Dim Card_B = PlayingCard.CreateNew(CardSuit.Diamonds, CardFace.Two)
Which is the Four of Spades.
This because enums are that they are evaluated as the underlying type, which my case it is the default Integer.
This also explains the need for the follow section of code in CreateNew
If Not [Enum].IsDefined(GetType(CardFace), Face) Then Throw New ArgumentOutOfRangeException If Not [Enum].IsDefined(GetType(CardSuit), Suit) Then Throw New ArgumentOutOfRangeException
If it was for this, it could be any valid integer.
What would be better if the enum type information was preserved and respected.
e.g. I defined that it should an CardFace and not an Integer.
Forgot to mention you can define an Enum which is treated like a sets of bits.
<Flags> Enum LowBit MiddleBit OtherBit HighBit End Enum
Dim myFlag = LowBit + HighBit + OtherBit
Note: A word of warning though. Don't change the ordering if you save a value containing an enum as it will change its meaning.
This post has been edited by AdamSpeight2008: 08 November 2010 - 01:00 PM