I am currently working on a project which creates a dictionary with an int index of the instance and a complex type as the value. Since this is a huge school project I do not want to post a ton of code as I have a logic problem rather than a “I need code”. I’ll try to be as clear as I can and if there is something I need to explain better please let me know.
First off. I have a dictionary in my server:
private Dictionary<int,List<complexType>> dictName = new Dictionary<int,List<complexType>>
Every time a client starts up it registers with the dictionary (i create a blank complex type to instantiate it then i load the dictionary):
List<complexType> temp = null;
dictName.Add(id,temp)
Then when the time comes that I want to add to the list for a particular instance I do this:
complexType myItem = new complexType();
dictName[id].Add(myItem);
When I run this code I get an error when a second client tries to run:
“An unhandled exception of type
‘System.Reflection.TargetInvocationException’ occurred in
mscorlib.dll. Additional information: Exception has been thrown by the
target of an invocation.
Now this happens when the second user hits:
dictName.Add(id,temp) from the first part.
If i change the instantiation of temp to List<complexType> temp = new List<complexType>();
then it passes that spot but I get the same error again when it updates the clients.
I am currently using this way of passing data with int and string (dictionary)
and they work fine but when I added in a List of a complex type in the dictionary I got the above error.
If anyone has any suggestions I would greatly appreciate it. I’m hoping it has something to do with my initial load of a blank list. If there is anything else you need to know please Ask Thanks!
You are making a dictionary of lists. So you were trying to add to a list that was null and that is what gave you the first exception.
Second, I’ve never seen the “new private Dictionary” is this a cut and paste typo?
This works:
The fact that you have list of a complex type and I have list of strings shouldn’t matter.