Search | | Contact Us | Resource Center Login | Home|

How would you write a method that returns three different strings?

Which answer is correct?

a) Use OUT parameters
b) Return a combined string with the three values delimited
c) Return a structure field containing three alpha fields
d) Return an ArrayList containing three strings
e) Return an instance of a class that has three string members
f) Any of the above


Correct answer: (f)

Each of these approaches will work in different situations, and they each have their advantages and disadvantages.

For (a), you could return one of the strings and use OUT parameters for the other two, or you could use OUT parameters for all three. The disadvantage to this approach is that the calling routine has to provide separate variables for each of the OUT parameters.

With (b), you can use a single return value, but you must parse the result in the calling program, as well as deal with potential conflicts between the delimiter and the contents of each component string.

A structure field (c) works nicely with Synergy/DE syntax, but each alpha component of the returned field must have a fixed length.

An ArrayList (d) can contain any number of any type of object. The disadvantages to this approach include having to extract each value from the ArrayList and cast each one as a string in order to use it.

With a custom class (e), not only can you use variable-length strings, but you can also reference them without casting if the members are public or the class provides accessor methods. The only disadvantage to this approach is that you must write this class and then instantiate and populate it in the method.

As you can see, Synergy/DE provides several options for solving any problem. There are probably even more ways to solve the example above than we have enumerated here. Which solution you choose depends on your specific needs.

More information about News and Events

Contact Synergex to learn more about  News and Events