Results 1 to 9 of 9
Thread: Returning Multiple Value's
- 10-22-2010, 01:24 AM #1
Member
- Join Date
- Oct 2010
- Posts
- 10
- Rep Power
- 0
Returning Multiple Value's
I don't know how to return more than one variable from a method. This code works, it returns val1 from the method get_Info(). However, what if I have a method that I want to return two variables? I want to know if it's possible to return val1 and val2 from the get_Info() method. How would I remove both variables and then put them into my run() method?
I apologize for the newbie questions... your help is appreciated.
public void run() {
int value1 = get_Info(5);
println("This is the first Value Returned " + value1);
}
private int get_Info(int x) {
int val1 = 5 + x;
int val2 = 10 + x;
return val1;
}
- 10-22-2010, 01:37 AM #2
You can only return one thing from a method. You could however create an object that has components val1 and val2 and then return the entire object. Or just create two separate methods and return each integer.
Sincerely, Joshua Green
Please REP if I help :)
- 10-22-2010, 01:54 AM #3
Member
- Join Date
- Oct 2010
- Posts
- 10
- Rep Power
- 0
Again, Thank you. That is my next question. I am very close to figuring this out. In the following code, I believe I have created the object brick and then returned the object to my run() method. If this is true...then how do I access the brick GRect object from the run() method.
ie: Let's say I want to change it's position. From my experience I can only do this within the method that created it.
public class ReturnObject extends GraphicsProgram {
public void run() {
drawBrick(20,20,50,50);
}
private GRect drawBrick(double x,
double y,
double x1,
double y1) {
GRect brick = new GRect(x, y, x1, y1);
add(brick);
return brick;
}
}
- 10-22-2010, 03:14 AM #4
You could set a GRect in the run method if I'm not mistaken:
Java Code:GRect brick = drawBrick(20,20,50,50);
Then if you wanted to change parts of the brick individually you would have to create new methods. A call to such a method would probably look like this if the method was called "setX" and had a GRect parameter:
Java Code:brick.x = setX(brick);
Let me know if this doesn't make sense and I'll try to explain further.Sincerely, Joshua Green
Please REP if I help :)
- 10-22-2010, 03:28 AM #5
Member
- Join Date
- Oct 2010
- Posts
- 10
- Rep Power
- 0
Josh, thank you for taking the time to help me out. That makes perfect sense. That's how I have been doing it. I am trying to right my code in such a way that I don't have to duplicate code, which is why I created the drawBrick() method. I wanted to somehow create the the brick in the method and then manipulate it in the run() method.
I guess that's not possible?
Chad
- 10-22-2010, 03:31 AM #6
So you want to be able to do something like...
Java Code:brick.x = 50;
I'm just trying to understand fully, and you're welcome, it's no trouble at all.Sincerely, Joshua Green
Please REP if I help :)
- 10-22-2010, 04:04 AM #7
Member
- Join Date
- Oct 2010
- Posts
- 10
- Rep Power
- 0
I want to be able to do the following code in the run() method
brick.x = setX(brick);
when the method that created the box was in drawbrick().
- 10-22-2010, 04:50 AM #8
Adding the following method should work then:
Java Code:private void setX(GRect brick) { x = 50; }
Also, if you could post an updated version of your code that would help a lot so I could see where you are at now.Sincerely, Joshua Green
Please REP if I help :)
- 10-22-2010, 09:09 AM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
What is a GRect?
Is something checking the values in that brick object created every so often to see if it's changed?
If there isn't some mechanism like that then simply changing them won't result in it moving.
Of course I don't actually know the framework you're using, so it's possible that stuff add()ed to a GraphicsProgram is checked in this way.
Similar Threads
-
Running multiple threads on multiple CPU cores?
By Dosta in forum Threads and SynchronizationReplies: 2Last Post: 09-19-2010, 03:48 PM -
returning arrays
By cjohnson412 in forum New To JavaReplies: 4Last Post: 11-25-2008, 01:30 PM -
Why is my list returning nothing?
By xcallmejudasx in forum New To JavaReplies: 2Last Post: 11-05-2008, 03:51 PM -
text box listeners and returning multiple strings from methods
By int80 in forum New To JavaReplies: 5Last Post: 07-18-2008, 04:30 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks