tags:

views:

583

answers:

8

Hi,

I have an enum which is used by multiple classes. What is the best way to implement this?

Thnx

+7  A: 

Put it in its own file and just declare it:

public enum Foo { a, b, c, d }

Consider your namespacing of course.

DarkwingDuck
+2  A: 
  1. Put the enums right in your namespace (or make a new namespace for them) or
  2. Put them in a (static?) class somewhere if that makes more sense.

I'd keep them in their own file for easy access and good organization either way.

I usually wouldn't put them in a class unless they somehow "belong" there. Like you have a Car class with an enum for the types of car, and a Road class and Bridge class that can set limits for types of car. Car.CarType seems to be a logical organization for this...

lc
How do i put an enum in my namespace? How do i create such a file?
Martijn
Create a new .cs file with: namespace MyNameSpace { public enum MyEnum {Value1, Value2, Value3} }
lc
So i just create a class?
Martijn
It's not a class just an enum in a namespace.
tpower
Enums don't have to be in a class. They CAN be, but don't have to be. Note Killersponge's response about following MS's lead as well.
lc
I don't get it. I have an cs file as you suggested. But when i type myEnum. i don't get the intellisense list with enum values and myEnum is declared public.
Martijn
If the namespace the enum is in is different from the namespace you're in, you need to "using" the enum's namespace.
lc
+9  A: 

Typically I just throw them into the namespace. It's what Microsoft did writing the .NET framework, so it's what I do too, for consistency you understand :)

Sekhat
nice, i didn't know you could do that
Gordon Carpenter-Thompson
A: 

Here's an example of having two enums in different namespaces:

using TotallyDifferentNameSpace.Enums;

namespace EnumEx
{
    class Program
    {
        static void Main(string[] args)
        {
            CarType car = CarType.Audi;
            PetrolType pType = PetrolType.Diesel;
        }
    }

    public enum CarType
    {
        Volkswagen,
        Audi,
        Toyota,
        Ford,
        Porsche,
        Lada
    }
}

namespace TotallyDifferentNameSpace.Enums
{
    public enum PetrolType
    {
        Gasoline,
        Diesel
    }
}
atsjoo
A: 

My advice is to not use Enums at all. Use singletons instead.

Like

    public class A : ISomeInterface
    {
        public static readonly A Instance = new A();
        private A()
        {
        }

    }

    public class B : ISomeInterface
    {
        public static readonly B Instance = new A();
        private B()
        {
        }
    }

In most cases that works better and is better to extend later.

Best wishes

Matze

Matze
This is somewhat similar to the type safe enum pattern, but its overengineering for no reason. Stay agile, refactor your enums later if its needed.
DarkwingDuck
Trouble starts if you need to extend your Enums later. One new value for customer A, another one for customer B. Boom! Ups! Both mapped to same int.Of course if you are 100% sure the Enum will never be changed ..
Matze
If you think the enums will change, you shouldn't use enumerations. Use classes instead.
Chuck Conway
+5  A: 

XML comments help, especially if others will use it

 /// <summary>
/// Defines the types of cars we support
/// </summary>
public enum CarType
{
    /// <summary>
    /// VW - the peoples car
    /// </summary>
    Volkswagen,

Your enum name should be plural (as instructed by FxCop)

public enum CarTypes

Add numeric values and use bitwise numbering even if you don't plan to use bitwise (it's a pain to renumber later).

    Volkswagen = 1,

    /// <summary>
    /// They own lambo... can't be all that bad
    /// </summary>
    Audi = 2,

    /// <summary>
    /// Good, cheap, reliable
    /// </summary>
    Toyota = 4,
Robert MacLean
+1  A: 

You can put an enum is a new codefile with .cs extension, for intellisense to work make sure its part of your project/solution and ofcourse it should be a public enum so that you have a solution scope for it. If intellisense is a problem , make sure you build your solution once, i had this problem once and just a rebuild solved it. Namespacing is a good option if you want to organize your code properly and you are coding a large project. The .NET framework was large. so it has enums under namespaces just for better understanding and code organization.

Sumit Ghosh
A: 

I know my suggestion goes against the .NET Naming conventions, but I personally prefix enums with 'E' and enum flags with 'F' (similar to how we prefix Interfaces with 'I'). I really do not understand why this is not the convention. Enums/Flags are a special case like Interfaces that will never change their type. Not only does it make it clear what it is, it's very easy to type in intellisense since the prefix will filter most other types/variables/etc, and you won't have these naming clashes.

And that would also solve another problem where for examples in WPF they use static classes like enums (e.g. FontWeights) that have pre-defined instances of types but you would not know if you don't search for it. If they just prefixed them with 'E', all you would have to do is type on character to find these special static classes.

Hermann