Question

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?

Was it helpful?

Solution

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;
}

OTHER TIPS

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top