S
S
Sergey Khlopov2017-10-05 17:20:50
Programming
Sergey Khlopov, 2017-10-05 17:20:50

How to create an array or collection of objects?

Hi everybody). I now have three objects of different classes.

NewClassProgram NewObject1 = new NewClassProgram();
NewClassProgram2 NewObject2 = new NewClassProgram2();
NewClassProgram3 NewObject3 = new NewClassProgram3();

And here's the actual question, if the objects are of different types, how can these objects be added to an array or collection ? It's just that if the objects are of different types, then it seems that they can't be added to the array, right?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
res2001, 2017-10-05
@res2001

Make a class hierarchy, inherit your classes from one ancestor.
Then you can add classes converted to a common ancestor to the collection.
Usually, when questions like this arise, the classes line up nicely in a hierarchy with a common ancestor.

M
mshak, 2017-10-05
@mshak

All objects in a sharp are inherited from the object type, so it is quite possible to create a
List collection or an object[] array.
But judging by your picture, you need more complex logic, something like Form.Controls

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question