Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I need to define enums in several classes. The majority of fields are the same in all of the enums. But one has one or two more fields, another has fewer fields. Now I wonder what is the best way to deal with this?

  1. Create separate enums

    public enum Foo {field1, field2, field5 };
    public enum Bar {field1, field2, field3, field4 };
    
  2. or use a general enum for them all

    public enum FooBar {field1, field2, field3, field4 , field5};
    

The enumerations contain the list of actions available for each class.

Update : sorry if the question was not clear enough . I want to use these enums as parameters in classes methods . for example classOne has a method as below :

public void DoSomeThing(Foo action,object moreData)
{
    // operation desired
}

here I want action to be limited to Insert , Update and Delete

while in classTwo :

public void DoSomeThingElse(Foo action)
{
    // operation desired
}

I want to limit action availabe to Insert and Select

share|improve this question
    
Could you group the related fields into the same enum but stil have multiple enums i.e. Enum { field1, field2 } Enum { field3, field4, field5 } etc? –  dreza Sep 29 '12 at 7:42
    
@dreza , sorry I can't get the point , can you explain more please –  reza Sep 29 '12 at 8:13
    
You say field1 and field2 are the same in he different enums Foo and Bar? Why not make a FooBar enum with these two values etc Hard to know as depends on what the enums actually represent and how they are used throughout your application I suppose. –  dreza Sep 29 '12 at 8:17
    
@dreza : enums contain list of actions available for each class . Foo has Insert , update and Delete while Bar just have Insert . –  reza Sep 29 '12 at 9:04
add comment

migrated from codereview.stackexchange.com Sep 29 '12 at 12:24

This question came from our site for peer programmer code reviews.

3 Answers

up vote 7 down vote accepted

You should use a different approach.

Create an enumeration which contains all available actions (insert, update, delete, etc.). So you can add a new (static) field to each class _ a list of items of that enumeration _ to represent the list of valid actions.

It makes no sense to have a separate enumeration for each class _ the enumerations semantically represent values of the same kind.

For example, you have an enumeration:

public enum ACTIONS { insert = 1, update = 2, delete = 3 }

In you class you would use it like this:

public class MyClass
{
    public static ACTIONS[] Actions = { ACTIONS.insert, ACTIONS.delete }; 
}
share|improve this answer
    
I updated the question . by the way I didn't get the point of static fields in each class , how ? –  mohsen.d Oct 1 '12 at 9:24
    
The actions are the same for any instance of the class, so there is no need for each instance to have that information _ you can get that information for any instance from the class itself. –  superM Oct 1 '12 at 10:02
    
can you give me an example ? –  mohsen.d Oct 1 '12 at 10:05
    
I've added an example to my answer –  superM Oct 1 '12 at 11:15
add comment

The enumerations contain the list of actions available for each class.

Why are you trying to use enums to solve your problem. I would create interface to cover all common behavior (actions) of my classes instead. You may create one ICommon interface which will contain all your common behaviors for the class to support your design and class behaviors.

There are very good posts on this topic like Designing C# Software With Interfaces or you may also reference Wikipedia for common information - The concept of class interface.

share|improve this answer
    
I updated the question –  mohsen.d Oct 1 '12 at 9:19
add comment

Conceptually, are enums talking about the same concept or thing or are they just related? For example, I might have a Direction enum and a Gears enum. Both may have a Reverse value but they mean very different things. In general, only merge concepts if they are the same. Otherwise you are making an assumption.

That said, from your comment above, it sounds like they are talking about the same concept (insert, update and delete) so I would merge them.

Also, if interop is a concern or they are similar, consider assigning the enum values fixed values, e.g. 1 for insert, 2 for delete and so on. It will make conversion and compatibility easier.

share|improve this answer
    
yes they are the same . can you explain more about fixed values and how this solves the problem ? thanks –  mohsen.d Oct 1 '12 at 9:08
1  
Having defined values (e.g Foo.Field1 is 1) and the same integer value for the same enum member in different enums (e.g. Bar.Field1 is also 1) makes conversion and interop easier. If you have to have separate enums, this makes converting between them straight forward rather than having lookup tables. –  akton Oct 1 '12 at 9:11
    
thanks , I updated the question to be more clear –  mohsen.d Oct 1 '12 at 9:20
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.