Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I put a variable in this part of my code?

Tags:

c#

I have two examples

Primary.Teal800, Primary.Teal900, Primary.Teal500, Accent.LightGreen400, TextShade.WHITE

and

Convert.ToInt32(textBox.Text)

The part after the . I want a variable. For example, the pseudo-code for this would be

var color = "Teal";
Primary.color + 800, Primary.color + 900, Primary.color + 500, Accent.LightGreen400, TextShade.WHITE

or

var toWhich = "ToInt32";
Convert.toWhich(textBox.Text)

Would there be any way to do that?

I'm accessing an enum

like image 570
Dgameman1 Avatar asked Dec 05 '25 07:12

Dgameman1


1 Answers

Since Primary is an enum, you can use Enum.Parse. Make a helper method for it:

static Primary GetPrimaryColor(string name, int number) {
    return (Primary)Enum.Parse(typeof(Primary), name+number);
}

Calling the helper lets you do this:

var color = "Teal";
GetPrimaryColor(color, 800), GetPrimaryColor(color, 900), ...
like image 147
Sergey Kalinichenko Avatar answered Dec 12 '25 17:12

Sergey Kalinichenko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!