Phil Ryan
Phil Ryan

Reputation: 885

how to set a property of an instance based on the value of a string?

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.

Upvotes: 0

Views: 241

Answers (3)

LVBen
LVBen

Reputation: 2061

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

Upvotes: 1

phoog
phoog

Reputation: 43056

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?

Upvotes: 0

Andrew
Andrew

Reputation: 5093

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

Upvotes: 0

Related Questions