【发布时间】:2016-05-16 13:59:51
【问题描述】:
我的项目设置中有大约 10 个 bool 类型的设置。我想要一个列表或数组来存储这些变量,但每当我更改某些数组/列表索引值时,我也需要设置值也应该更改。我目前有他们这样声明:
private static bool[] CombinationAchievements =
{
Properties.Settings.Default.GetStraight,
Properties.Settings.Default.GetFlush,
Properties.Settings.Default.GetFullHouse,
Properties.Settings.Default.GetFourOfAKind,
Properties.Settings.Default.GetStraightFlush,
Properties.Settings.Default.GetRoyalFlush
};
但是,当我更改 CombinationAchievements[0] = true 时,Properties.Settings.Default.GetStraight 的值仍然等于 false。我可以创建一个方法来编辑设置,但它大多是硬编码的:
private void Test(bool[] editSettings)
{
properties.settings.default.GetStraight= editSettings[0];
properties.settings.default.GetFlush= editSettings[1];
.
.
.
}
只是一个伪代码。但是,如果我有 100 个设置,它看起来一点也不好看。我需要的只是可以保存我所有设置的东西,所以函数看起来像这样:
private void Test(bool[] editSettings)
{
List<properties.settings.default> thisIsNotHowYouDoItMate = new List<properties.settings.default>
for(int i = 0;i<thisIsNotHowYouDoItMate.count;i++)
{
thisIsNotHowYouDoItMate[i]=editSettings[i];
}
}
上面的代码是我想要实现的,当然这是废话,但我希望你明白了。
【问题讨论】:
-
只要你必须调用 100 个属性设置器,它就永远不会漂亮。 System.Configuration 是微弱的,NameValueCollection 几乎和它一样好。通过声明一个枚举使其变得更好。
-
那么现在有办法将它们存储在数组中吗?同样使用枚举,每次我正确时我都必须强制转换 (int) :?
-
也许您最好使用自己的配置,使用 xml 序列化文件?
-
在我的情况下 xml 序列化如何提供帮助:?
标签: c# arrays winforms resources boolean