Is there a way to dynamically create/delete objects? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Is there a way to dynamically create/delete objects?

My problem is the following: I want to write a program which manages a database of e.g. persons. It's up to the user to create them. Ergo it is unknown how many objects are needed. Is there a way to "automatically" assign names to the objects (Foo0, Foo1, Foo2...)? I already have te idea to put all objects into a List so the way to delete an object would just be BarList[42].Finalize(); BarList.RemoveAt(42);. Any ideas?

5th Jan 2017, 12:14 AM
Maike
2 Answers
+ 4
Have you tried using ExpandoObject? See https://www.oreilly.com/learning/building-c-objects-dynamically To delete them you can perhaps tag it and use something like foreach (Label label in Controls.OfType<Label>()) { if (label.Tag != null && label.Tag.ToString() == "dynamic") label.Dispose(); }
5th Jan 2017, 12:27 AM
Alex
Alex - avatar
+ 2
Thanks! That will help me. I'll get into it tommorow.
5th Jan 2017, 12:33 AM
Maike