根据字符串保存设置

时间:2012-07-06 12:55:32

标签: c# settings

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

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

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

  

错误2:作业的左侧必须是变量,属性或索引器。

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

1 个答案:

答案 0 :(得分:2)

您正在寻找

if / elseswitch

无法连接变量名称!

所以你的解决方案是这样的(如果colorOptionNametoSave是一个字符串):

if(colorOptionNametoSave == "Blue")
{
    Properties.Settings.Default.Blue = selectedIndexString;
}
else if(colorOptionNametoSave == "Red")
{
    Properties.Settings.Default.Red = selectedIndexString;
}