1

I have an enum

private enum EventColors
            {
                Aquamarine,
                Azure,
                BurlyWood,
                CadetBlue,
                Gainsboro,
                Gold,
                Gray,
                Khaki,
                LawnGreen,
                LightGreen,
                LightSkyBlue,
                Linen,
                MediumOrchid,
                MediumPurple,
                MistyRose,
                Olive,
                OliveDrab,
                Orange,
                OrangeRed,
                Orchid,
                PaleTurquoise,
                Peru,
                Pink,
                Plum,
                RoyalBlue,
                SandyBrown,
                SeaGreen,
                SteelBlue,
            };

I chose the best from System.Drawing.Color and I would like to randomly choose one:

Array values = Enum.GetValues(typeof(EventColors));
                Random rnd = new Random();
                EventColors randomBar = (EventColors)values.GetValue(rnd.Next(values.Length));

How can I convert random chosen color from my enum to System.Drawing.Color. ? Is this possible without using switch?

Cezar
  • 345
  • 6
  • 18
  • `rnd.Next(int)` return an int, when your enum start from 0 then you could cast directly to your enum type. – AlirezaJ Feb 18 '17 at 19:28
  • But System.Drawing.Color is not an Enum, so I can't use my randomized int to get color value. – Cezar Feb 18 '17 at 19:30
  • 2
    Then you should specify a mapping between your enum to System.Color. Maybe a calculation function or dictionary lookup. – AlirezaJ Feb 18 '17 at 19:31

2 Answers2

3

You can create Dictionary<EventColors, System.Drawing.Color>, fill it by this way:

Dictionary<EventColors, System.Drawing.Color> colors = new Dictionary<EventColors, System.Drawing.Color>();

colors.Add(EventColors.Aquamarine, System.Drawing.Color.Aquamarine);
colors.Add(EventColors.Azure, System.Drawing.Color.Azure);
//... other colors

and then:

Array values = Enum.GetValues(typeof(EventColors));
Random rnd = new Random();
EventColors randomBar = (EventColors)values.GetValue(rnd.Next(values.Length));

System.Drawing.Color someColor = colors[randomBar];

OR

You can use reflection:

Array values = Enum.GetValues(typeof(EventColors));
Random rnd = new Random();
EventColors randomBar = (EventColors)values.GetValue(rnd.Next(values.Length));

string name = Enum.GetName(typeof(EventColors), randomBar);
var type = typeof(System.Drawing.Color);
System.Drawing.Color systemDrawingColor = (System.Drawing.Color)type.GetProperty(name).GetValue(null);   
Roman
  • 11,966
  • 10
  • 38
  • 47
0

Adding onto Roman's answer, here's something I use to keep the mapping self-contained with the enum:

using System.Drawing;

public enum EventColors
{
    Aquamarine,
    Azure,
    BurlyWood,
    ...
}

public static class EventColorsExtensionMethods
{
    public static readonly IReadOnlyDictionary<EventColors, Color> ColorMap = new Dictionary<EventColors, Color>()
    {
        { EventColors.Aquamarine,  Color.Aquamarine },
        { EventColors.Azure,       Color.Azure      },
        { EventColors.BurlyWood,   Color.BurlyWood  },
    };

    public static Color GetDrawingColor(this EventColors color)
    {
        return ColorMap[color];
    }
}

This way the caller can use the following:

System.Drawing.Color color = EventColors.Aquamarine.GetDrawingColor();

For randomization, see How do I select a random value from an enumeration?

Anil
  • 655
  • 1
  • 11
  • 25