Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Accessing Objects From Vectors

Status
Not open for further replies.

Descartes

Technical User
Apr 13, 2002
3
GB
I want to be able to store a number of objects and access them, as in with an array or vector. The problem is that you can't use methods of objects while they're in the vector (you can't say: myObjects.elementAt(i).myMethod() ) because while something's in a vector you can only use the methods of the generic 'Object' class. I don't want to use an array because I need to have a variable number of objects at any time. The problem gets even worse when you have a collection of objects which each have within them a collection of objects and you want to access them and their methods. If anyone can tell me what I'm missing or what other collection types I can use I'd be real grateful.
Cheers Descartes
 
You need to cast the object to the correct type is all, as in (MyObjectType)myObjects.elementAt(i).myMethod() . I honestly haven't tried this so I don't know if it will work on the fly like you are trying to do. It is only one extra step to cast it the correct type and store it in a local variable and shouldn't cause any additional overhead as in

MyObjectType mot = (MyObjectType)myObject.elementAt(i);
mot.myMehtod();

Anyway, hope that was helpful.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top