When I find the need to return more than 1 value from a single function call, I approach the solution similar to the way .Net events pass arguments around (with the System.EventArgs class). I employ a return argument class. Just create a class with the items you wish to return and return an instance of the class.
I often use this technique for methods that need to return data but could also result in a failed attempt. Throwing exceptions is more expensive and you typically know what the expected errors/failures might be, so you can define that into the return argument class. Using a discrete class provides strong typing to your return data that you can't get from an object array.
I usually define a base return argument class for some basic return values:
- Success/Fail boolean
- an exception for thrown exceptions
Then I extend it to make it specific to the methods that will return it (similar to how the framework extends the EventArgs class to be specific to the event delegates that will pass it).
For certain return arg classes, I'll create and include an error code enumeration and even some data of the necessary type.
-
Peter