The only way to return more than one value from any method is to use an object.
If any of the parameters to your method are objects and the instance variables are mutable, you can change these fields -- but you can't change the object-reference variables that are passed in as parameters.
Arrays are objects as are Collections and any other object that is mutable can have more than one of its instance variables changed by your method.
You can only return only one value in a return statement, but since this returned value can be an object, you can return an object that constists of more than one value by creating an object in your method and then returning the object that your method has created.
I'd avoid collections and Object solutions as they hide the meaning of the method. I'd rather create a custom object just to hold the two or three things the method might modify. For example, if you want to return x and y coordinates, int would be a bad return type, Point would be a good one.
If you can't find a descriptive name and make a cohesive object to return, you probably have some other design issues that ought to be ironed out, like maybe the method is doing too much.
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
I agree that you don't want to make up classes that are unnecessary, but I remember that a long time ago, I worked with a software engineer that choose to use the Point datatype in Classic MacOS, programming in Pascal.
Unlike the new Point datatype in Java, this old datatype of the same name used 16-bit signed integers and once it was stored on disk, we were stuck with a 32K limit.
We should have defined our own type, let's say called "cellID" for a spreadsheet-like object. Then, since we owned the type, we could have used 32-bit integers and had a limit of 4 billion rows.
It seems that even Microsoft made the same mistake as we did. They had a limit of 64K rows in Excel. That's a lot, but I know of a consultant that wanted to take a books-in-print database consisting of 3 million rows and import it in Excel and massage the data to be in a form to input into a database program.
With Excel's 64K limit, this meant that he'd have to reformat the data about 50 times instead of just once. He also had to figure out a way to break the data file into pieces.
I have three points, if you use an existing type, make sure that it really is going to be sufficient, and 2) if you ever store a type to disk, make sure that you're not "painting yourself into a corner", and 3) is you make up your own types, your code may be more readable, saving you time and money.
Another option is to introduce a Method Object - a class that has only the method returning just one of the values (or even void), and getters for all the other values you need to retrieve.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com
subject: Is it possible to return multiple values from methods