Computer Science Canada

returning multiple values

Author:  ilovechicken [ Tue May 17, 2005 7:43 pm ]
Post subject:  returning multiple values

when i have written a method and want it to return multiple values to the method that called it how do i do it? the only way i know so far is in the method that is doing the calling write a line like;

variable = methodName ();

and in method name:

public boolean methodName ()
{
return true;
}

but this can only return one variable at a time...

Author:  wtd [ Tue May 17, 2005 8:04 pm ]
Post subject: 

You either have to return an array, or some other collection or write a class yourself that wraps several values.

Something like:

Java:
class Pair<T1, T2>
{
   private T1 val1;
   private T2 val2;

   public Pair(T1 iv1, T2 iv2)
   {
      val1 = iv1;
      val2 = iv2;
   }

   public T1 first()
   {
      return val1;
   }

   public T2 second()
   {
      return val2;
   }
}


Convenient, isn't it?

:: silently points to all of the languages that *can* do this easily ::


: