- Home /
How to access the data in a list that save any class inside
I need to acces a data from a list of any class inside of this list
how you can do that?
List<object> dynamic_list;
dynamic_list.Add(node);
dynamic_list[i].node.current_pos; // dont work when you access like a normal list, list<normal_class> normal_list
Answer by Bunny83 · Sep 29, 2019 at 04:38 AM
Well all your elements in your list are references of type object. You have to manually cast the reference to the proper type in order to access any type specific fields, properties or methods of that type. So either:
YourNodeType n = (YourNodeType)dynamic_list[i];
n.node.current_pos;
Or just inline by using brackets:
((YourNodeType)dynamic_list[i]).node.current_pos;
Note that using an untyped array / list can cause a lot issues. First of all casting requires runtime type checking which can be quite slow if you use this often. Second if you try to cast an "object" to the wrong type your code will throw an exception and will terminate the execution. Why do you actually use an untyped list? If you want to mix different types, how have you planned to figure out what kind of object is in which element? What kind of different data you want to store in that list? What is the actual reason why you need those different types in one list? Do those different objects have anything in common?
Hi @Bunny83 thanks for your answer, I wanted to know, how to make a network of list, and conect list with other list, like a pointers in c++, in the most dynamic way possible , but for now this is the most close way that I know, so how you can create a struct or list or type or without type thing that can save and conect data very dynamically like Hyper Procedural Dynamic List on Unity?
@JuanForero I edited my answer on your original topic (https://answers.unity.com/questions/1668265/i-neet-to-make-like-a-hyper-procedural-dynamic-lis.html) to add an example on how to typecast your elements in the list. However, if what you are trying to achieve is a list of list, you can just do it like so in C#:
List<List<YourType> > listNestingLists = new List<List<YourType> >();
Your answer
![](https://koobas.hobune.stream/wayback/20220612212747im_/https://answers.unity.com/themes/thub/images/avi.jpg)