• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

passing class name rather then object

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Darren Tweedale
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for confusing everyone! I have requested to delete my previous "incompleted" topic, but I think Nathaniel solved my problem with
object.class.getClass();
many thanks.
 
Nathaniel Stoddard
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Darren,
How's this?
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Nathaniel Stoddard
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Darren Tweedale
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic