tags:

views:

241

answers:

3

I'm trying to set up an enumeration that maps certain project-specific values to the standard System.Drawing.Color enum.

Here's the idea of what I'd like to do:

public enum SessionColors
{
     Highlights = Color.HotPink,
     Overlays   = Color.LightBlue,
     Redaction  = Color.Black   
}

The goal is to have it so I can use SessionColors.Highlights for things I identify as a highlight and yet, I can change the enumeration mapping later and affect all subsequent colors.

I realize I can look up the values of Color.HotPink, etc.. and just use those but it's not as clear. Any better idea out there?

+6  A: 

Just do it with public constants:

public static class SessionColors
{
    public static readonly Color Highlights = Color.HotPink;
    public static readonly Color Overlays   = Color.LightBlue;
    public static readonly Color Redaction  = Color.Black;
}
Jon Grant
Just to augment this rather than posting my answer, you *can* map enum values in your own enum to the values in another enum. What you're dealing with here, as Hans says, is not an enum value, it's a Color (which is a struct).
Adam Robinson
I just assumed Color was an enum, didn't actually check it. This solution works great, thanks!
Nick Gotch
I can't see the advantage of using static readonly fields rather than constants for this problem.
Mehrdad Afshari
Constants are compiled inplace. Static readonly may be replaced at runtime on instantiation of the object (is this case it would have to be in the static constructor). As well as they can be updated in future versions without recompiling everything that uses them.
Matthew Whited
Static readonly would allow me to change the mapping in the assembly and not have to concern myself with a separate assembly using the older mapping.
Nick Gotch
I understand but the OP says "project-specific values" so that's out for this example. Anyway, I just wanted to recommend against blindly use `static readonly` fields where `const` makes more sense.
Mehrdad Afshari
Try it, you get this error: The type 'System.Drawing.Color' cannot be declared const.
Jon Grant
+3  A: 

Colors.HotPink is not an enum value, it's a static property of the static class Colors that returns a Color value. And that Color value is a struct, not an integer.

So you can't use a Color as underlying value of an enum, as that is restricted to the integral types.

Hans Kesting
+1  A: 

I would personally do it with Color properties in a static class instead of Enumerations. There are many advantages to this, but possibly the most beneficial would be that this could allow you to load the colors from app.config (or some other configuration source) at runtime, without forcing a recompilation.

Reed Copsey