Tutorial :Is there a way to map an enumeration to another enumeration in C#.NET 3.5?



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?


Solution:1

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


Solution:2

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.


Solution:3

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.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »