7 Replies - 232 Views - Last Post: 06 April 2013 - 07:51 PM Rate Topic: -----

#1 mouthwash  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 12-February 13

Reflection with return method

Posted 06 April 2013 - 12:40 PM

So I am trying to use reflection to set an arraylist of points to the set of points returned by another method.
It looks like this:
ArrayList<Point> Positions = new ArrayList<Point>();
Method method = Class.forName("Maps").getMethod(map,null);
Positions = method;


but this doesn't work.
I know that my method works properly because if i do:
Positions = Maps.map1;
it works as intended.

Any ideas on how to do this?

Is This A Good Question/Topic? 0
  • +

Replies To: Reflection with return method

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10816
  • View blog
  • Posts: 40,317
  • Joined: 27-December 08

Re: Reflection with return method

Posted 06 April 2013 - 12:48 PM

A Method and a List<Point> are two different objects entirely. Why are you using reflection here? It seems unnecessary.
Was This Post Helpful? 0
  • +
  • -

#3 mouthwash  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 12-February 13

Re: Reflection with return method

Posted 06 April 2013 - 12:57 PM

how would i then dynamically set the values of the List Positions based on a string name. To be a bit more clear, I have a function like this
public void CreateMap(String myMap){
ArrayList<Point> Positions = new ArrayList<Point>();
Positions = Maps.map1;
}

What I want to do is instead of always setting Positions to Maps.map1, i want to set it to whatever the string myMap is called. Through a bit of research i found that the way to do this is through reflection, but if there is a better way, I'm all ears.

Also, the method is returning a list of Points, so they are not different things.
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10816
  • View blog
  • Posts: 40,317
  • Joined: 27-December 08

Re: Reflection with return method

Posted 06 April 2013 - 01:03 PM

It sounds like good OO practice would be better here. Tell us more about the design of your project and what exactly you're trying to build, more than just this specific component.

At the very least, a Map<String, List<Point>> seems like a better way to do things. Reflection is not the answer here.
Was This Post Helpful? 0
  • +
  • -

#5 mouthwash  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 12-February 13

Re: Reflection with return method

Posted 06 April 2013 - 01:14 PM

View Postmacosxnerd101, on 06 April 2013 - 01:03 PM, said:

It sounds like good OO practice would be better here. Tell us more about the design of your project and what exactly you're trying to build, more than just this specific component.

At the very least, a Map<String, List<Point>> seems like a better way to do things. Reflection is not the answer here.

hmm, how would i used the Map<String, List<Point>> never seen that before.
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10816
  • View blog
  • Posts: 40,317
  • Joined: 27-December 08

Re: Reflection with return method

Posted 06 April 2013 - 01:31 PM

It relates key-value pairs. So the name of the map is the key, and the actual grid (the List<Point>) is the value. So you can get(mapName) on the Map and it will return the List<Point>.
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10816
  • View blog
  • Posts: 40,317
  • Joined: 27-December 08

Re: Reflection with return method

Posted 06 April 2013 - 01:37 PM

Just to add as well- I'm not sold a Map is the best solution here. You just haven't given us a clear enough description of your project that I can really help and offer a cleaner design.
Was This Post Helpful? 1
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

Re: Reflection with return method

Posted 06 April 2013 - 07:51 PM

Agree
Reflexion is useless here so even the Map design can be over doing it
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1