0

我需要根据输入字符串保存到不同的设置。为什么这条线不起作用?

Properties.Settings.Default + colorOptionNametoSave = selectedIndexString;
Properties.Settings.Default.Save();

其中colorOptionNametoSave是不同的颜色设置,selectedIndexString是要保存的值。但是我收到消息:

错误 2:赋值的左侧必须是变量、属性或索引器。

我能想到的唯一解决方法是switch声明,但我有很多颜色,所以这会很长。关于更有效的解决方案的任何想法?

4

1 回答 1

2

if/else或 aswitch是您要查找的内容。

不能连接变量名!

因此,您的解决方案将是这样的(如果colorOptionNametoSave是字符串):

if(colorOptionNametoSave == "Blue")
{
    Properties.Settings.Default.Blue = selectedIndexString;
}
else if(colorOptionNametoSave == "Red")
{
    Properties.Settings.Default.Red = selectedIndexString;
}
于 2012-07-06T12:57:52.920 回答