[Webinar] Streamline your web hosting managementRegister Today


Enum standards / naming conventions

Posted on 2008-01-24
Medium Priority
Last Modified: 2008-03-24

Have couple questions about enums
First off I tend to nest my enums in my classes like...

public class Address
    // Enums
    public enum AddressType { Home, Billing, Alternative };

    // Member Variables
    private AddressType m_Type;

    // Properties
    public AddressType Type
        get { return m_Type; }
        set { m_Type = value; }

Is this good, bad, or just comes down to preference?
Where is the most logical place for it if it's not nested, same namespace as class?

My biggest issue seems to be that I always seem to want to name my Property the same as my Enum but can't cause of name collision.  For example in the above code sample i really wanted to name the property AddressType instead of Type.  It just seems natural to me that the enum should be named AddressType, but at the same time i also think the property should be named AddressType.

I checked MSDN naming conventions but the info didnt seem to help me.
Maybe my problem is just the fact that i should not be nesting my enums to begin with.
What would the correct standard be? Or at least what works well for you?

If you don't nest them what file do you put them in, all enums in same file or would you put it in the same file as the class it pertains to, If it doesnt pertain to more than 1 class that is.  So in my example above, lets say my AddressType enum will only be used in Address class, should i just put in that file, just not nested in the Address class?

What about an enum that is used in multiple classes, where best place for it to live?

Sorry for so many questions, I may be splitting hairs here but just a little confused on the best way to structure things, or at least the most commonly used way, since there may not be a "best" way.


Question by:krazykoder
LVL 22

Expert Comment

ID: 20737717
I think it's natural to place an enum that's associated with one class inside that class. In this case, you could:
public enum TYPE { Home, Billing, Alternative };

Then external references can be Address.TYPE.Home. The capitalization is unfortunate, but you might have trouble with Type. Then, your property can be:
    // Properties
    public TYPE AddressType
        get { return m_Type; }
        set { m_Type = value; }


Author Comment

ID: 20738007
i appreciate your feedback JimBrandley.

However I would rather stick with what i have then deal with that uppercase TYPE, I understand Type is probabaly a keyword, but at any rate that is just flip flopping what i already have.

I was kind of looking for a more global solution since i seem to run into this same exact scenario many times.  I can always solve it one way or another by giving the enum and property a different name, but was tryign to change my coding practices to prevent from running into this same scenario over and over.

At the same time don't want to break any of the conventions that are the norm.

Enum names should be PascalCase
Property names should be PascalCase

I'm almost tempted to suffix my nested enums with Enum to prevent the name collision.
enum AddressTypeEnum
public AddressTypeEnum AddressType

only thing is the naming convention on MSDN says not to do that :(
LVL 22

Accepted Solution

JimBrandley earned 375 total points
ID: 20738429
Naming can be a headache. We keep enums that relate to a single class inside that class. We have one very large one that relates to many classes, and that one lives in its' own class. That's
OUROBJECTS.TYPE.  (Note - this predates our current standards of PascalCase).

Then properties that serve up a value of that type are called ObjectType. So, you can play with the names some by combining class name and enum name.  That's tough if you already have the class name in the enum name.

One thing we have done when not happy with names is to try to pick a synonym. For example, Address.Kind or Address.Use. It's a bummer when the one you want is a keyword.


LVL 55

Expert Comment

by:Jaime Olivares
ID: 20740499
I use to name enumeration names starting with E, like EType or EAddrType

Expert Comment

ID: 21196405
Not sure why there is an "Accepted Solution" that only offers up sympathy to the problem.  The specific questions are:

Is it a bad convention to use the same name for everything?  AddressType enum, AddressType property - the point is, it COMPILES - so what no one is actually saying is "yes, do it that way", and why not?  None of the enum naming guidelines suggest anything to distinguish the enum from the property.

Second - where to put enums?  In the class, in the namespace?  Seems like scope may be part of the answer, as in, if it is only used internally to the class, put the enum in the class, but if it is needed by external objects, put it in the namespace.  There is also the question of whether to put the enum in the same file as the class, or to have a separate file containing all of your enums.

I would hope that an expert would adress all these concerns and provide reasoning and justification for these choices, or perhaps point out that it is a matter of personal preference and/or style that will drive these decisions, as there does not seem to be any guideline that I can find.

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Hi, this video explains a free download that you can incorporate into your Access databases, or use stand-alone for contact management. Contacts -- Names, Addresses, Phone Numbers, eMail Addresses, Websites, Lists, Projects, Notes, Attachments…
Free Data Recovery software is an advanced solution from Kernel Tools to recover data and files such as documents, emails, database, media and pictures, etc. It supports recovery from physical & logical drive after a hard disk crash, accidental/inte…
Suggested Courses

612 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question