从列表中分配字符串

时间:2014-04-21 21:56:01

标签: c# string

我创建了一个包含很多选项的列表,让我们说每个字符都有不同的颜色。

string A = "#FFB97FC9";
string B = "#FF9BCC50";
// etc.
string answer1 = Options.Answer1;
answerRectangle.Fill = GetColorFromHexa(answer1);

现在让我们说Options.Answer1 = A

我希望answerRectangle拥有代码#FFB97FC9

的颜色

我怎样才能做到这一点?

注意:我想要answer1 = #FFB97FC9

2 个答案:

答案 0 :(得分:2)

使用字典而不是变量:

var colors = new Dictionary<string, string>();
colors["A"] = "#FFB97FC9";
colors["B"] = "#FF9BCC50";
// etc.
string answer1 = Options.Answer1;
answerRectangle.Fill = GetColorFromHexa(colors[answer1]);

答案 1 :(得分:1)

为什么不用选项创建字典:

Dcitionary<string, string> colors = new Dictionary<string, string>();
colors.Add("A", "#FFB97FC9");
colors.Add("B", "#FF9BCC50");

然后:

answerRectangle.Fill = GetColorFromHexa(colors[answer1]);