473,387 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Bad practice with Enumerations....

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 )
Oct 9 '06 #1
6 1179
Robinson wrote:
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
If it makes sense in your object model to have a MaxValue for an
enumeration, I don't see that it is bad practice. I think more common
is an enumeration value that is a combination of one or more values,
especially when the enumeration is used as a set of flags.

<FlagsPublic Enum Options
NoOptions = 0
Option1 = 1
Option2 = 2
Option3 = 4
Option4 = 8
AllOptions = 15
End Enum

Often, the 'AllOptions' value is expressed like this:

AllOptions = Option1 And Option2 And Option3 And Option4

Chris

Oct 9 '06 #2
Hello Chris,

I think you meant to use the OR operator.

-Boo
Robinson wrote:
>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
If it makes sense in your object model to have a MaxValue for an
enumeration, I don't see that it is bad practice. I think more common
is an enumeration value that is a combination of one or more values,
especially when the enumeration is used as a set of flags.

<FlagsPublic Enum Options
NoOptions = 0
Option1 = 1
Option2 = 2
Option3 = 4
Option4 = 8
AllOptions = 15
End Enum
Often, the 'AllOptions' value is expressed like this:

AllOptions = Option1 And Option2 And Option3 And Option4

Chris

Oct 11 '06 #3
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 )

Oct 12 '06 #4
Otherwise MaxValue would be one more then the actual MaxValue!

Actually Jay, the way I use the sentinel value (nice term) is so I can loop
with something like:

For i As Integer = 0 To MyEnum.MaxValue - 1

Next

However, you are right, it's much better if using sentinels to make it equal
the last term in the enum.
Rather then relying on sentinel value, framework developers should perform
the check using one of the real enum values.
This is fine imho, however during development I'm often moving values about,
adding, removing or renaming them. It's very easy to forget to update the
max value in this case This is another reason I just made MaxValue 1 + the
last value in 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.
Yes this is a good reason not to use it.
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...
Ah, yes. This is much better. Thanks for the tip.
Oct 13 '06 #5
GhostInAK wrote:
Hello Chris,

I think you meant to use the OR operator.

-Boo
You are correct sir. You are a credit to ghosts everywhere.

Thanks

Oct 13 '06 #6
Hello Chris,

booahahahaha! heh.
I'll try not to haunt ya.

-Boo
GhostInAK wrote:
>Hello Chris,

I think you meant to use the OR operator.

-Boo
You are correct sir. You are a credit to ghosts everywhere.

Thanks

Oct 13 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Joyce | last post by:
In my schema I have 2 enumerations, let's say, country description and country code, and I want to use them so I can map each country description to its precise country code (and no other). So far...
0
by: Plinkerton | last post by:
I'm making an Base Class that will be inherited. In my base class, I have a public enumeration that defines a list of things I want my class to be able to do. I use it for Method input...
21
by: Christopher Benson-Manica | last post by:
I'll try to explain what I want to do: I have foo.h and foo.cpp. Units that include foo.h will define an enumeration bar: enum bar { typeNone, typeBaz, typeQuux, ... , count }; A method...
1
by: someone else | last post by:
I have some code that creates dynamic enumerations for use in a PropertyGrid control. This all works perfectly but the memory usage of the program increases quite quicly when viewing the...
1
by: Oleg Ogurok | last post by:
Hi all, I've added a new DataSet (xsd file) to my project in VS.NET 2003. There I create a simple type as an enumeration of values. <xs:simpleType name="MyCustomType"> <xs:restriction...
4
by: ChrisB | last post by:
Hello: I will be creating 50+ enumerations related to a large number of classes that span a number of namespaces. I was wondering if there are any "best practices" when defining enumerations. ...
27
by: Ben Finney | last post by:
Antoon Pardon wrote: > I just downloaded your enum module for python > and played a bit with it. IMO some of the behaviour makes it less > usefull. Feedback is appreciated. I'm hoping to...
77
by: Ben Finney | last post by:
Howdy all, PEP 354: Enumerations in Python has been accepted as a draft PEP. The current version can be viewed online: <URL:http://www.python.org/peps/pep-0354.html> Here is the...
9
by: Jacek Dziedzic | last post by:
Hi! I often find that my programs need to store information on "current mode of something" with two or at most several mutually exclusive "modes" to choose from, e.g. - datafile: is it in a)...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.