473,387 Members | 1,348 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.

Managing Enumerations

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.

Should a single class be created that contains all of a solution's
enumerations? Or should enumerations be defined directly in the files that
contain the related classes? Are there any other design patterns I am
overlooking?

Any thoughts?

Thanks!
Chris
Nov 16 '05 #1
4 3516

Perhaps others have differing opinions, but I've always kept things that are
global, global. That is to say that if an enumeration or constant is used in
several "random" places through out a project, then a global scope is a good
place for these to exist in.

If I find these global instances can be grouped together (a load of XML
constants for example) then I create a class within this Global class that
seperates and factors out this group. (Global.Xml)

If the constants are specific to a class and are not and should not be
referenced by other objects, then they belong to that class and I'd keep
them encapsulated there. Again grouping if the number of constants grows
sufficiently large.

I'd like to hear Mr Skeet's view on this.

Thanks
Dan.
"ChrisB" <pl****************@thanks.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
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.

Should a single class be created that contains all of a solution's
enumerations? Or should enumerations be defined directly in the files that
contain the related classes? Are there any other design patterns I am
overlooking?

Any thoughts?

Thanks!
Chris

Nov 16 '05 #2
<"Dan Bass" <danielbass [at] postmaster [dot] co [dot] uk>> wrote:
Perhaps others have differing opinions, but I've always kept things that are
global, global. That is to say that if an enumeration or constant is used in
several "random" places through out a project, then a global scope is a good
place for these to exist in.

If I find these global instances can be grouped together (a load of XML
constants for example) then I create a class within this Global class that
seperates and factors out this group. (Global.Xml)

If the constants are specific to a class and are not and should not be
referenced by other objects, then they belong to that class and I'd keep
them encapsulated there. Again grouping if the number of constants grows
sufficiently large.

I'd like to hear Mr Skeet's view on this.


Well, there are two different questions here: how the enums should be
grouped as far as the CLR is concerned, and how they should be grouped
in source code. As far as the CLR is concerned, I keep enums which are
only concerned with one class in that class, whether or not they should
be used by other classes - for instance, in an HTTP class (a layer over
WebRequest, effectively) I had a property which said which protocol to
use - the type was an enum, which was contained within that class
because it was only meant to be used *for* that class, even though
other classes would be specifying it. Other enums I treat as normal
types.

In terms of source code, I often group several small sets of enums
together, in a file called Enums.cs (only those in the same namespace,
of course). I do the same for delegates too.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3
Well, there are two different questions here: how the enums should be
grouped as far as the CLR is concerned, and how they should be grouped
in source code. As far as the CLR is concerned, I keep enums which are
only concerned with one class in that class, whether or not they should
be used by other classes - for instance, in an HTTP class (a layer over
WebRequest, effectively) I had a property which said which protocol to
use - the type was an enum, which was contained within that class
because it was only meant to be used *for* that class, even though
other classes would be specifying it. Other enums I treat as normal
types.


Whereas, personally, I woudl keep that enum external to the class because,
while the enum is only used *in* the one class, it will be used almost
exclusivly by other classes.

Its pretty tough to find consensus on this, I think.
Nov 16 '05 #4
Thanks to everyone for their input.

Chris

"Dan Bass" <danielbass [at] postmaster [dot] co [dot] uk> wrote in message
news:ez**************@TK2MSFTNGP15.phx.gbl...

Perhaps others have differing opinions, but I've always kept things that
are global, global. That is to say that if an enumeration or constant is
used in several "random" places through out a project, then a global scope
is a good place for these to exist in.

If I find these global instances can be grouped together (a load of XML
constants for example) then I create a class within this Global class that
seperates and factors out this group. (Global.Xml)

If the constants are specific to a class and are not and should not be
referenced by other objects, then they belong to that class and I'd keep
them encapsulated there. Again grouping if the number of constants grows
sufficiently large.

I'd like to hear Mr Skeet's view on this.

Thanks
Dan.
"ChrisB" <pl****************@thanks.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
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.

Should a single class be created that contains all of a solution's
enumerations? Or should enumerations be defined directly in the files
that contain the related classes? Are there any other design patterns I
am overlooking?

Any thoughts?

Thanks!
Chris


Nov 16 '05 #5

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...
3
by: JoeH | last post by:
Hi, I'm using a COM DLL (created in VB) in my javascript code and can successfully call its methods and get/set its properties. There are also some Public enumerations defined in the ActiveX...
5
by: Seamus M | last post by:
I can't find any info on enumerations in the PHP manual, so I assume there is no built in way to create them. Can anyone tell me the best way to build a simple enumeration, such as: Enum...
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...
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.