Robinson,
I'm sure you meant:
Public Enum MyEnum
FirstValue = 0
SecondValue
ThirdValue
MaxValue = ThirdValue
End Enum
Otherwise MaxValue would be one more then the actual MaxValue!
The .NET guidelines themselves suggest that you avoid sentinel values in
enums. Unfortunately I don't see an online reference to the reason, it is
however in the printed copy of the book "Framework Design Guidelines -
Conventions, Idioms, and Patterns for Reusable .NET Libraries" by Krzysztof
Cwalina & Brad Abrams from Addison Wesley.
<quote>
4.8 Enum Designs
- Do Not include sentinel values in enums.
although they are sometimes helpful to framework developers, they are
confusing to user of the framework. Sentinel values are values used to track
the state of the enum, rather then being one of the values from the set
represented by the enum.
....
Rather then relying on sentinel value, framework developers should perform
the check using one of the real enum values.
</quote>
In your words, your check should look for ThirdValue itself rather then
MaxValue, likewise you check should look for FirstValue also. (in case a
negative number was converted to the Enum).
The users or your enum (including your predecessors) won't know if MaxValue
should be used or not. Hence you should avoid including it.
Dim theRandom As New Random(1234)
Dim theEnumValue As MyEnum = CType ( theRandom.Next ( 0,
MyEnum.MaxValue ), MyEnum )
I would use Enum.GetValues to get the list of allowed values, then get a
random index into this list to retrieve a random value. Ensure that it will
work with Enums that are not zero based. Hint: Generally I make FirstValue =
1, and have 0 be a None value, so my code causes an error if I pass an
uninitialized enum value...
Dim theValues() As MyEnum =
DirectCast([Enum].GetValues(GetType(MyEnum)), MyEnum())
Dim theRandom As New Random(1234)
Dim theEnumValue As MyEnum = theValues(theRandom.Next(0,
theValues.Length))
--
Hope this helps
Jay B. Harlow
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley -
http://www.tsbradley.net
"Robinson" <to******************@myinboxtoomuchtoooften.comwr ote in
message news:eg*******************@news.demon.co.uk...
Hi,
I have a vauge recollection that setting a "MAX" member of an enumeration
is bad practice. I can't for the life of me remember or think why this
might be so and would appreciate your thoughts on doing this:
Public Enum MyEnum
FirstValue = 0
SecondValue
ThirdValue
MaxValue
End Enum
... and perhaps choosing a random value from the enumeration, such as:
Dim theRandom As New Random(1234)
Dim theEnumValue As MyEnum = CType ( theRandom.Next ( 0,
MyEnum.MaxValue ), MyEnum )