I have some string constants that I need to access from multiple files. Since the values of these constants might change from time to time, I decided to put them in AppSettings rather than a constants class so that I don't have to recompile every time I change a constant.
Sometimes I need to work with the individual strings and sometimes I need to work with all of them at once. I'd like to do something like this:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="CONST1" value="Hi, I'm the first constant." />
<add key="CONST2" value="I'm the second." />
<add key="CONST3" value="And I'm the third." />
<add key="CONST_ARR" value=[CONST1, CONST2, CONST3] />
</appSettings>
</configuration>
The reasoning being that I'll then be able to do stuff like
public Dictionary<string, List<double>> GetData(){
var ret = new Dictionary<string, List<double>>();
foreach(string key in ConfigurationManager.AppSettings["CONST_ARR"])
ret.Add(key, foo(key));
return ret;
}
//...
Dictionary<string, List<double>> dataset = GetData();
public void ProcessData1(){
List<double> data = dataset[ConfigurationManager.AppSettings["CONST1"]];
//...
}
Is there a way to do this? I'm pretty new to this and I concede that this might be horrendous design.
You do not need to put array of key in
AppSettingskey as you can iterate all keys of AppSetting from code itself. So, yourAppSettingsshould be like this :After this, you can create global static dictionary which you can access from all part of the program :
As
Foo methodhas been accessed fromstaticproperty, you need to define Foo method as static method. so, your Foo method should look like this :Now, you can access Dataset
dictionaryby its key in following way :