0

I've been searching for this a lot, but I haven't found anything close to what I need, and maybe it's something simple, but the things that I get are only how to concatenate strings using "+" or the Format procedure.

What I would like to know is how to call a string that contains the HEX format of a color by changing another string.

Heres and example:

I have named the following strings:

string Color_Option1 = "#FF17868B";
string Color_Option2 = "#FFFFFFFF";
string Color_Option3 = "#FF000000";

And this variable:

private static string m_optionclicked = "";
        public static string OptionClicked
        {
            get { return m_optionclicked; }
            set { m_optionclicked = value; }
        }

Then I have 3 buttons and one TextBlock, and when a button is clicked they put a value on "OptionClicked", ex:

OptionClicked = "Option2";

So depending on which button is clicked I want to change the background color of the TextBlock like this:

TextBlock.Background = (SolidColorBrush)(new BrushConverter().ConvertFrom("Color_" + OptionClicked));

But I get and error everytime on the background line, does anyone knows how can I call the string called "Color_Option2" with a similar syntaxis?

I've made sure that OptionClicked has the value "Option2" and it actually works if I write "Color_Option2" instead of concatenating the string and variable.

BTW, this is a simplified example, I have a lot of buttons, textblocks, and colors, so making a chain of "If"s or determining one by one would make my life miserable.

Thanks in advance.

2
  • 6
    Seems like you might want to consider storing this information in a dictionary to allow for easy retrieval based on the option name. Also, you might want to consider including the error if you want any sort of real answer. Commented Jun 27, 2016 at 19:45
  • Sorry, the error that pops up is "FormatException was unhandled - An unhandled exception of type 'System.FormatException' occurred in PresentationCore.dll" But I supposed that it was because I was calling the string in a wrong way. And I'm new to C# but what I searched about storing information in a dictionary seems to be sort of the same as how I'm storing my information, but the problem would still be on how to call it. I might be wrong so if I am, please tell me, I still want to learn a lot more of C# Commented Jun 27, 2016 at 20:28

1 Answer 1

0

If you want to do this through reflection, you could have a class with public fields, such as

class ColorClass
{
    public string Color_Option1 = "#FF17868B";
    public string Color_Option2 = "#FFFFFFFF";
    public string Color_Option3 = "#FF000000";
}

and then to grab the values,

ColorClass colorClass = new ColorClass();
(string)typeof(ColorClass).GetField("Color_" + OptionClicked).GetValue(colorClass);

The other way of solving this is as David L said in comments, just create a dictionary with the key as your string variable name.

Sign up to request clarification or add additional context in comments.

2 Comments

This certainly works, although it is a shame to incur reflection overhead for this.
Thanks a lot, it worked, just in case someone else wants to try it, this is how i did it. ColorClass colorClass = new ColorClass(); / TextBlock.Background = (SolidColorBrush)(new BrushConverter().ConvertFrom((string)typeof(ColorClass).GetField("Color_" + OptionClicked).GetValue(colorClass))); Although it seems like a lot of work for just this, in VB its way easier.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.