I have a struct
struct myStruct {
Dictionary<string, int> a;
Dictionary<string, string> b;
......
}
I want to create a arraylist of that struct
ArrayList l = new ArrayList();
myStruct s;
s.a.Add("id",1);
s.b.Add("name","Tim");
l.Add(s);
However, I got the error “Object reference not set to an instance of an object.”
Anyone could tell me why?
Thanks.
Some suggestions to improve your code:
Don’t use a
struct, use aclassinstead. Structs in .NET are a little different and unless one understands those differences I doubt one will ever have a valid use for structs. Aclassis almost always what you want.ArrayListis more or less obsolete, it’s almost always better to use a genericList<T>instead. Even if you need to place mixed objects in the list,List<object>is a better choice thanArrayList.Make sure your members are properly initialized and not
nullbefore you access methods or properties of them.It is better to use properties instead of public fields.
Here is an example: