我需要根据输入字符串保存到不同的设置。为什么这条线不起作用?
Properties.Settings.Default + colorOptionNametoSave = selectedIndexString;
Properties.Settings.Default.Save();
colorOptionNametoSave
是不同的颜色设置,selectedIndexString
是要保存的值。但是我收到了消息:
错误2:作业的左侧必须是变量,属性或索引器。
我能想到的唯一工作是switch
声明,但我有很多颜色,所以这会很长。有关更有效解决方案的任何想法吗?
答案 0 :(得分:2)
if
/ else
或switch
。
您无法连接变量名称!
所以你的解决方案是这样的(如果colorOptionNametoSave
是一个字符串):
if(colorOptionNametoSave == "Blue")
{
Properties.Settings.Default.Blue = selectedIndexString;
}
else if(colorOptionNametoSave == "Red")
{
Properties.Settings.Default.Red = selectedIndexString;
}