C# How to make settings array WinForms -


i have around 10 settings in project settings of type bool. have list or array store these variables need whenever change of array/list indexes value setting value should change. have them declared :

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     }; 

however when change combinationachievements[0] = true properties.settings.default.getstraight value still equal false. can create method edit settings it's hardcoded :

private void test(bool[] editsettings) {     properties.settings.default.getstraight= editsettings[0];     properties.settings.default.getflush= editsettings[1];     .     .     .  } 

just pseudo code. doesn't nice @ imagine if have 100 settings.. need can hold settings function can :

private void test(bool[] editsettings) {     list<properties.settings.default> thisisnothowyoudoitmate = new list<properties.settings.default>     for(int = 0;i<thisisnothowyoudoitmate.count;i++)     {         thisisnothowyoudoitmate[i]=editsettings[i];     }      } 

the above code im trying achieve of course crap hope got idea.

so if want go custom settings xml file storage here example how implemented:

using system; using system.collections.generic; using system.diagnostics; using system.io; using system.linq; using system.xml.serialization;  namespace xmlconfigurationexample {     public sealed class configuration     {         private readonly list<setting> _settings = new list<setting>();         private readonly object _lock = new object();         private readonly xmlserializer _serializer;         private const string filename = "settings.xml";          public configuration()         {             _serializer = new xmlserializer(typeof(list<setting>));             loadsettings();         }          public object this[string key]         {                         {                 setting setting = _settings.firstordefault(s => s.key == key);                 if (setting != null)                 {                     return setting.value;                 }                 return null;             }             set             {                 setting setting = _settings.firstordefault(s => s.key == key);                 if (setting != null)                 {                     lock (_lock)                     {                         setting.value = value;                     }                     savesettings();                 }             }         }          private void savesettings()         {             try             {                 using (filestream filestream = file.open(filename, filemode.openorcreate, fileaccess.write, fileshare.write))                 {                     _serializer.serialize(filestream, _settings);                 }             }             catch (exception ex)             {                 // here real error handling                 debugger.log(100, "error", ex.message);                 throw;             }          }          private void loadsettings()         {             if (!file.exists(filename))             {                 return;             }              try             {                 using (filestream filestream = file.open(filename, filemode.open, fileaccess.read, fileshare.read))                 {                     ((list<setting>)_serializer.deserialize(filestream)).foreach(s => _settings.add(s));                 }             }             catch (exception ex)             {                 // here real error handling                 debugger.log(100,"error",ex.message);                 throw;             }         }          public void addsetting(string key)         {             if (_settings.all(s => s.key != key))             {                 _settings.add(new setting { key = key });             }         }          public void removesetting(string key)         {             setting setting = _settings.firstordefault(s => s.key == key);             if (setting != null)             {                 _settings.remove(setting);             }         }     } } 

setting model:

using system.runtime.serialization;  namespace xmlconfigurationexample {     [datacontract]     public class setting     {         [datamember]         public string key { get; set; }         [datamember]         public object value { get; set; }     } } 

and usage in e.g. console application:

configuration configuration = new configuration(); configuration.addsetting("test"); configuration.addsetting("test2");  configuration["test"] = "value1"; configuration["test2"] = 12.040;  // here real output or using of configuration debugger.log(100, "log", configuration["test"].tostring()); debugger.log(100, "log", configuration["test2"].tostring()); 

configuration should instantiated once in application , used e.g. singleton reading , writing.


Comments

Popular posts from this blog

routing - AngularJS State management ->load multiple states in one page -

python - GRASS parser() error -

Swift game error message -