Accessing complicated dictionaries

danhodge

In Runtime
Messages
324
Location
UK
Just a heads up, this is related to an assignment, I know the forum has rules about that, but I'm hoping the fact i'm not asking anyone to do it for me, and am instead just asking a generic question, gets around that(if not, I sincerely apologize).

So, I have a complicated dictionary that is storing data. Each instance is a new module, which has its own categories, which has its own list of values, so this is the dictionary I have:
Dictionary<string, Dictionary<string, List<string>>> modules = new Dictionary<string, Dictionary<string, List<string>>>();

The dictionary inside it is compromised of:
Dictionary<string, List<string>> categories = new Dictionary<string, List<string>>();

I'm pretty certain i'm adding values to it correctly, but I'm a tiny bit confused as to how I would then access the individual variables. For example, i just want to test they are adding properly now, so this is what i want to do:
foreach(var module in modules) {
//debug module name
foreach(var category in categories) {
//debug category name
foreach(string value in values) {
//debug all values in category
}
}
}

My confusion comes into the fact that i've no idea what datatype each of those vars would be, and I've no idea how to reference the dictionary inside modules :(

I'm using C#.

Thanks for reading,
Danny
 
Last edited:
Going to suggest this: instead of Dictionary of Dictionary, why not do a DataTable or a DataSet (can contain multiple datatables)? Or even just create your own object(s), and then make a List of that object type?
 
No problem. Sometimes it just takes a little rearchitecting rather than trying to continue with something that's not working :D.
 
Yeah, what I had did look a bit ridiculous :p
Quick question; I've been making console applications for ages, and the output part of visual studio was showing the line number when an error appeared during debugging. This assignment is a forms application, and when i get an error with this I don't see a line number anywhere, and it doesn't pause the program or anything. Do you know what's going on with that?
 
Yeah, what I had did look a bit ridiculous :p
Quick question; I've been making console applications for ages, and the output part of visual studio was showing the line number when an error appeared during debugging. This assignment is a forms application, and when i get an error with this I don't see a line number anywhere, and it doesn't pause the program or anything. Do you know what's going on with that?

It should still throw an exception if a fatal error is occurring, and tell you what line it happened on (and take you do that line).

Are you running as a Debug application or a Release application? If you're running as Release, it won't debug or hit any breakpoints. Switch to Debug and it should work.

And make sure you're placing breakpoints so you're not relying on errors themselves to take you where the error happens :p. I hope you know about breakpoints, at least lol.
 
Right now, the exception I keep getting is Exception thrown: 'System.ArgumentOutOfRangeException' in mscorlib.dll, but it won't give me any more info, take me to a line or highlight it or anything. I've also tried sticking a try catch around the part where I know the error is coming from, and still no line numbers or anything.

And its in debug mode, and I'm gonna have to start using them now I guess :p
Console applications are so good at telling me what's gone wrong :(
 
Like I said, I've done Console / Winform / WPF / ASP.NET / MVC apps - and they all tell me what line the error threw on in the code.

Look at the stacktrace for the Exception that's generated - should tell you where it blew up at.

Also, what version of VS are you running?
 
Ahh, the errors I was having must have not been fatal, it's now pausing the program and taking me to the line where the exception occurred.
Technically, it still isn't giving a line number in the output, but oh well, if it takes me there I can't complain too much ;)
Visual Studio 2015 :)
Thanks for all the help, needless to say there's a lot of stress here, closing in on deadlines. Not fun :(
 
Back
Top Bottom