0

Apologies if this has been answered before, but I've been reading and searching and I can't find the answer.

I have an array, let's say string[] myColors = {"red","white","blue"}; and I have selected one of those colors, e.g. by accessing the nth member of the array: myColors[2] which would be the string "blue".

Separately, I have a class, box, of which I have created an instance, myBox. The class has a property, boxColor, of type Color. And the possible values of that Color type include Color.red = RGB(255,0,0), Color.white = RGB(255,255,255), and Color.blue = RGB(0,0,255) (although for the sake of the argument, the actual values and types of these enumerated values are irrelevant, they could equally be float's or any other C# type).

How would I go about setting the myBox.boxColor to the value that I get from myColors[2]?

i.e. something like myBox.boxColor = (Color) (value of the string myColors[2]);

In this case, so that I can actually draw on the screen in that particular color.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Phil Ryan
  • 855
  • 1
  • 10
  • 16
  • Are you asking how to convert a string into RGB values? – bstar55 May 31 '14 at 04:56
  • @bstar55 no. I'm trying to set the value of a property using the text of the string. It could be a property like transcendental numbers (e, pi etc), and I would be wanting to set the particular transcendental number to the string value, e.g. "pi". – Phil Ryan May 31 '14 at 04:59
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackoverflow.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders May 31 '14 at 05:23

3 Answers3

1

You have to have some type of mapping. I use a Dictionary here, but there are alternatives. I used Color based on your question, but you could substitute Color with any other class or structure of your choosing.

   public struct Color
   {
      int r, g, b;
      public Color(int r, int g, int b)
      {
         this.r = r;
         this.g = g;
         this.b = b;
      }
   }

   public static class BetterColors
   {
      static Dictionary<string, Color> colorDictionary = new Dictionary<string, Color>();

      static BetterColors()
      {
         colorDictionary.Add("Red", new Color(255, 2, 4));
         colorDictionary.Add("Blue", new Color(0, 3, 251));
         colorDictionary.Add("Green", new Color(0, 200, 0));
      }

      static public Color GetColor(string colorName)
      {
         return colorDictionary[colorName];
      }
   }

   class Box
   {
      public Color boxColor { get; set; }
   }

   class Program
   {
      static void Main()
      {
         string[] myColors = { "Red", "Green", "Blue" };

         Box myBox = new Box();
         myBox.boxColor = BetterColors.GetColor(myColors[1]);
      }
   }
LVBen
  • 2,041
  • 13
  • 27
0

There is a built-in .NET function for this very thing, called Color.FromName:

Color blue = Color.FromName("Blue");

http://msdn.microsoft.com/en-us/library/system.drawing.color.fromname.aspx

Andrew
  • 4,953
  • 15
  • 40
  • 58
  • Beat me by less than a minute. :-) – S. Ahn May 31 '14 at 04:57
  • Sounds good for the particular case of colors, but I'm looking for the more general case. I'm trying to think of an example that doesn't involve colors. And wishing that I didn't use colors in my question. :-) – Phil Ryan May 31 '14 at 05:03
0

For the general case, you need a way of converting from the string to a value of the correct type. If there is no function analogous to FromName then you might use an enum or a class with static fields, and convert the strings to values using reflection. Another approach would be to use a dictionary to map the values.

In other words, to solve the problem generally, you need an element in the solution that is type-specific. Otherwise, for example, how would you convert from the string "three" to the int 3?

phoog
  • 42,068
  • 6
  • 79
  • 117
  • 1
    Thanks and also to @LVBen. This is probably why I was struggling with finding an answer. Of course a mapping has to exist somewhere. And this was starting to become clear to me as I was trying to construct a non-Color-based example. Appreciate the explanation & example. – Phil Ryan May 31 '14 at 05:57