Hello, I have a small problem which I hope you folks can solve. Basically I have a class that has a vector of different type of objects (organisation, person, supplier, whatever). I want to be able to get an object by its CLASS NAME not by object.
as you can see, I am just adding an object to the X object using the method add. X.add(new Organisation()); X.add(new Person()); now I want to get the object from the X class using the class name (not the object) ie. X.get(Organisation); -- Organisation is the class - I want it to return me the object Organisation that was added to the vector X.get(Person); -- Person is the class - I want it to return me the object Person thats was added to the vector Is this possible? Any suggestions would be appricated. Thanks Darren Tweedale
posted 15 years ago
Darren, Why are you double-posting? I'd recommend you back to the original post and edit it if you wanted to change the post. But this one should be deleted. You're just going to end up confusing alot of people, or annoying them.
This is a puzzling requirement. What if an organization had a hundred people, so you have 101 objects in your Vector. Which one should you retrieve when you get("Person")? Maybe we should be exploring richer data structures. Like a Vector for each classname and a Map to hold the Vectors. If you haven't run into Map yet, look at the API. It's a collection like Vector but you can get things out by a key like classname. So you could get the Vector of people using the key "Person" All this talk of using classname is very dynamic. It will hold anything in the world, Oranizations, People, Toasters, Mini Coopers, etc. I wouldn't encourage this kind of generic storage thingy unless you really need to handle any kind of unforseen data. If you have a model of a business with Organization and Person, maybe we should make a less dynamic object model of that particular structure.
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
posted 15 years ago
Stan, Maybe you should try something simpler like using a separate collection for each of the things that you are interested in. Consider using a collection for your "Persons", another collection for your "OtherThings", and so on. Trying to cram everything into one container will just end up being a nightmare for you later down the road.
Thanks for that Nathaniel - that was exactly how I did it - it worked a treat. As for stan, I see what you mean, but I have discounted for that, as this vector will have only one organisation or one person at any one time - its part of a login in system. Lately, I had to put in a new entity (third-party=someone acting on their behalf) which this "third-party" can also be a person, an organisation or just on its own. For example, I log in with my id, and I am a person as well as a third-party for someone else, therefore I should have two entities in the vector (person and third-party) So I am just trying to be flexable by using classes for any future unknown entities. Thanks once again for all help. Darren
Let's get him boys! We'll make him read this tiny ad!
how do I do my own kindle-like thing - without amazon