tags:

views:

66

answers:

2

I have an enum, which:

  • is included in my class as a property
  • it represents some values from a database table (a couple of types)
  • it is displayed in DropBox, so that it can be used as a filter

Now I would like to add 'All' (or 'None' for example) value to this DropBox.

How should I do this:

  • add 'All' value to Enum?
  • add 'All' value to DropBox, change type of my property from Enum to String
  • some other option...
+4  A: 

Codesleuth comment on another answer made me read the question again and here is an update.

Consider the use of a flags enumeration if you are going to have multiple combination's. In your case it would mean that selecting any combination of types is a valid input.

[Flags]
enum MyTypes
{
    None = 0,
    One = 1,
    Two = 2,
    Three = 4,
    Four = 8,
    All = One | Two | Three | Four
}

If the user can only select one type or all the types then use a normal enumeration:

enum MyType
{
    None,
    One,
    Two,
    Three,
    Four,
    All
}
João Angelo
+2  A: 

IMHO, it's best to add an 'All' value to your enum like so:

enum SampleEnum 
{
    Value1 = 1,
    Value2 = 2,
    Value3 = 4,
    All = Value1 | Value2 | Value3 
}

This way, you won't have to care about the displayed items in your combobox, and you can react to the selection of that value in your code, if that should be necessary...

Thomas Weller
+1, I agree. If you have no use for ever combining the other values, this is the best approach.
Codesleuth
You don't need to use Flags attribute?
_simon_
@_simon_: The FlagsAttribute is informational only. I suggest to include it, though, whenever you intend to combine the enum values (i.e. use them as flags). I'm not sure if this changes the ToString() behaviour for combined values.
OregonGhost
@_simon_: In the above case, no. You only need the [Flags] attribute if you need flexible assignments with combinations for that you have no predefined enum value (e.g. something like 'someValue = Value1 | Value3;').
Thomas Weller
Thanks, good to know :)
_simon_