dynamic - reading into an enum from xml in c# -
i have project working on in c# uses enum.
at moment looks . . .
public enum temp { carea = 10, careb = 20, carec = 22, cared = 35 }
however instead of call same data (as enum) either .txt file or .xml file. either do. save re-build every time have add entry enum (this happens frequently) - easier edit .txt or .xml file.
instead of using enum changes dynamically @ runtime suggest use dictionary.
class myclass { private dictionary<string, int> tempvalues = new dictionary<string, int>() { { "carea", 10 }, { "careb", 20 }, { "carec", 22 }, { "cared", 35 } } public dictionary<string, int> tempvalues { { return this.tempvalues } } }
you still need load values file , fill them:
private void readvalues(string path) { foreach(string line in file.readalllines(path)) { string[] tokens = string.split(','); string key = tokens[0]; int value = int.parse(tokens[1]); // todo: check if key not exist this.tempvalues.add(key, value); } }
your input file need this:
carea,10 careb,20 carec,22 cared,35
Comments
Post a Comment