How to create a dictionary from an external settings file that can be used by other classes?

I want to create a dictionary from a settings file that is formatted as a list of strings "somekey = somevalue". Then I want this dictionary of keys and values ​​generated by one class to be available for other classes in my program, so I don’t need to access an external file every time I want to use the settings in another class.

I computed the first part by creating a class that can read an external file and convert a list of strings to a dictionary, but I cannot figure out how to make dictionary data created by reading a class file accessible by other classes in the same namespace.

+4
source share
3 answers

A slightly different approach is to use the extension method, my example is pretty simple, but it works great

using System.Collections.Generic; namespace SettingsDict { class Program { static void Main(string[] args) { // call the extension method by adding .Settings(); //Dictionary<string, string> settings = new Dictionary<string, string>().Settings(); // Or by using the property in the Constants class var mySettings = Constants.settings; } } public class Constants { public static Dictionary<string, string> settings { get { return new Dictionary<string, string>().Settings(); } } } public static class Extensions { public static Dictionary<string, string> Settings(this Dictionary<string, string> myDict) { // Read and split string[] settings = System.IO.File.ReadAllLines(@"settings.txt"); foreach (string line in settings) { // split on = var split = line.Split(new[] { '=' }); // Break if incorrect lenght if (split.Length != 2) continue; // add the values to the dictionary myDict.Add(split[0].Trim(), split[1].Trim()); } return myDict; } } } 

Contents settings.txt

 setting1=1234567890 setting2=hello setting3=world 

And the result

Result

You must, of course, extend this with your own protective functions and the like. This is an alternative approach, but using extension methods is not so bad. The functionality of the Extensions class can also be implemented directly in the properties method in the Constants class. I did it for fun :)

+1
source

Just create a class that contains the public opinion constant and the dictionary in this class is static, so

 public class MyClass { // ... public static Dictionary<string, string> checkSumKeys { get; set; } // ... } 

call it like

 // ... foreach (KeyValuePair<string, string> checkDict in MyClass.checkSumKeys) // Do stuff... 

Or, if the dictionary is not made static, you need to instantiate the class

 public class MyClass { // ... public Dictionary<string, string> checkSumKeys { get; set; } // ... } 

call it like

 MyClass myclass = new MyClass(); foreach (KeyValuePair<string, string> checkDict in myClass.checkSumKeys) // Do stuff... 

Hope this helps.

+1
source

I'm not quite sure what you are doing here. Could you make the dictionary a public property of this class?

Well, one option is to use a public property, and then create one instance of this class when initializing your application (this will populate your dictionary if you populate it in your class constructor), and then you can pass the same instance to functions or class constructors without need to read the external file again.

 public class ReadFileClass { //Can be replaced with auto property public Dictionary<string, string> Settings { Get{return Settings} Set{Settings = value} } public ReadFileClass() { //In this constructor you run the code to populate the dictionary ReadFile(); } //Method to populate dictionary private void ReadFile() { //Do Something //Settings = result of doing something } } //First class to run in your application public class UseFile { private ReadFileClass readFile; public UseFile() { //This instance can now be used elsewhere and passed around readFile = new ReadFileClass(); } private void DoSomething() { //function that takes a readfileclass as a parameter can use without making a new instance internally otherfunction(readFileClass); } } 

Having done this, you can use only one instance of the object to populate the settings dictionary, and then simply pass it on. I have used this method many times to avoid multiple accesses to a database or file, which can have a costly performance impact. If you want to use a class that contains settings in a different file than the one that creates it, you simply force the class constructor to treat it as a parameter.

+1
source

All Articles