how to implement class with collection of string/object pairs so that an object can be returned with
Posted
by matti
on Stack Overflow
See other posts from Stack Overflow
or by matti
Published on 2010-04-06T14:13:58Z
Indexed on
2010/04/06
14:23 UTC
Read the original article
Hit count: 344
The values in a file are read as string and can be double, string or int or maybe even lists. An example file:
DatabaseName=SomeBase Classes=11;12;13 IntValue=3 //this is required! DoubleValue=4.0
I was thinking something like this:
public static T GetConfigValue(string cfgName) { // here we just return for example the value which could // be List[int] if parameter cfgName='Classes' // and LoadConfig was called with Dictionary containing // keyvaluepair 'Classes' / typeof(List[int]) } public static bool LoadConfig(Dictionary reqSettings, Dictionary optSettings) { foreach (KeyValuePair kvPair in reqSettings) { if (ReadCheckAndStore(kVPair, true)) return false; } foreach (KeyValuePair kvPair in reqSettings) { if (ReadCheckAndStore(kVPair, false)) return false; } return true; } private static bool ReadCheckAndStore(KeyValuePair kVPair, bool isRequired) { if (!ReadValue(kVPair.Key, out confValue) && isRequired) //req. IntValue !found return false; //here also have to test if read value is wanted type. //and if yes store to collection. }
Thanks a lot & BR! -Matti
PS. Additional issue is default values for optional settings. It's not elegant to pass them to LoadConfig in separate Dictionary, but that is an other issue...
© Stack Overflow or respective owner