Results 1 to 3 of 3

Thread: [RESOLVED] Return multiple values

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2001
    Posts
    574

    Resolved [RESOLVED] Return multiple values

    Hello,

    I just read that it is impossible to alter a parameter passed to a function. But I need a function to return 2 values. An integer and a string. Since I cannot use a parameter does anyone have a solution to return 2 values in a recursive function?

    Kind regards
    Don't Hate Me Cause You Ain't Me

  2. #2
    Fanatic Member
    Join Date
    Jan 2006
    Posts
    710

    Re: Return multiple values

    Use a separate class to encapsulate the two values and this class will be the return type of the method.

    e.g.,
    Code:
    public class Foo
    {
        int i;
        String s;
    }
    your method:
    Code:
    public Foo returnsFoo()
    {
        ... return a new instance of the Foo class
    }
    If you need to alter alread-existing values, then just use a void method with a parameter of type 'Foo'.
    Code:
    public void AltersFoo(Foo f)
    {
        f.i = 2;
        f.s = "new string";
    }
    This is perfectly valid since Java still allows you to modify the object via its public interface - you just can't reset the top reference 'f'.
    Last edited by David Anton; Oct 4th, 2009 at 07:02 PM.
    David Anton
    Convert between VB, C#, C++, & Java
    www.tangiblesoftwaresolutions.com

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2001
    Posts
    574

    Re: Return multiple values

    Thanks man, I have it working!
    Don't Hate Me Cause You Ain't Me

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width