Gurus!
I have a need to dynamically generate dictionaries in my C# code depending on the appearance of certain values in a text file that I am parsing. My text file contains comma separated values of the classes, student ids and student names from a school, a little bit like the following:
4, 3, John (class, sstudent id, student name)
3, 3, Jane
4, 4, Harry
2, 3, Harry
3, 5, Jenny
2, 1, Joe
.
.
What I need my C# code to do is the following:
Parse through the above input file, when it sees a new class at the beginning of each line, dynamically create a dictionary for that class, and add the student id and name in that line as a key - value pair. As parsing continues, add further students of the class as key - value pairs to the same dictionary. And as it comes across more classes while parsing the file, dynamically create dictionaries for those classes, and add student ids and names to them as k - v pairs.
For e.g., when it begins to parse the above sample data, it would dynamically create a dictionary like the following static creation:
Dictionary <int, string> class4Dictionary = new Dictionary <int, string>();
and then, add "3" and "John" as the first k - v pair:
class4Dictionary.Add(3, "John");Next, it would dynamically create a dictionary called class3Dictionary and add (3, "Jane") as an element. Once a dictionary has been create for a class, and the application encounters a student record for that class later in the file, that record is added to the dictionary.
But I am unable to figure out a way how to dynamically create a dictionary Gurus, seeking your wisdom for that!
Novice Kid