2012-07-06 49 views
0

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

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

colorOptionNametoSave是不同的颜色设置和selectedIndexString是保存价值。但是,我收到以下消息:

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

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

回答

2

if/elseswitch是你在找什么。

不能连接一个变量名!

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

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

ARG确定我只是希望一个更优雅的方式。将开始编写300+的情况开关块,然后......哈哈。 – ikathegreat 2012-07-06 13:12:43