|
-
Nov 13th, 2002, 03:30 PM
#1
Multiple return values
I saw this question asked here by someone: "So if you're a real guru, do you know how to return multiple values from a method?" or something like that.
Happens that I need just now. I have a function that should return both a boolean and a String. I don't want to write a class for this.
I suppose I can't do this:
Code:
boolean method(String out)
{
out = "Hello there!";
}
as out is a reference to an object, but the = in the function will just change out to reference a different object, not actually changing the object (impossible for String) or the reference that was originally passed to the function.
As I am writing I've just stumbled upon the java.lang.ref package, I think it's what I need.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Nov 13th, 2002, 03:33 PM
#2
No, that doesn't help me at all...
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Nov 13th, 2002, 03:44 PM
#3
Why can't you just flip them?
Code:
String method(Boolean out)
{
out = new Boolean(true);
return "Hello";
}
Or pass in a Hashtable with the variables you need?
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Nov 15th, 2002, 06:18 AM
#4
That would still not yield the desired result, it would still only set out to reference a new object, not change the object itself or change the reference that was passed in.
Stupid language...
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Nov 15th, 2002, 06:44 AM
#5
Addicted Member
I don't think there is any way of doing this. I think you will have to create a data object that can be populated and returned by the method. I know you don't want to but I've had to do it myself.
If anyone knows a better way I'd be VERY glad to hear it.
Sorry
HD
-
Nov 15th, 2002, 07:07 AM
#6
What a stupid language...
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Nov 15th, 2002, 07:16 AM
#7
I know that Strings destroy the current reference and reference a new object every time they're altered, but I thought that a StringBuffer worked differently, referencing the same object for the life of the variable. Maybe a StringBuffer is what you need?
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Nov 15th, 2002, 10:19 AM
#8
Yes, I could probably use a StringBuffer, but I'm not really happy with this solution...
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Nov 15th, 2002, 10:20 AM
#9
I could also use an array with just one element, but it's still only a workaround.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|