user3650460
user3650460

Reputation: 1

How do I create and name objects when I run my program?

Is it possible to have my program name objects as they're created? Such as using a loop that names objects object1, object2, etc.. This is an issue because it won't always be clear how many objects are necessary, it depends on the users input. Or is there another way to get around this without having to name the objects?

Upvotes: 0

Views: 48

Answers (2)

CharlieS
CharlieS

Reputation: 1452

You can create a hashtable of names and objects, which you append to as you create each object.

If the objects are of the same class, you can add an attribute to the class to contain the name.

Upvotes: 0

Juned Ahsan
Juned Ahsan

Reputation: 68715

No you cannot create references dynamically as you have mentioned but you can always use a collection of objects. If you know the number of objects upfront, then go for Array otherwise go for a List for dynamic number of objects.

Upvotes: 1

Related Questions