How do you return two values from a single method?

后端 未结 22 653
谎友^
谎友^ 2020-12-11 00:58

When your in a situation where you need to return two things in a single method, what is the best approach?

I understand the philosophy that a method should do one t

相关标签:
22条回答
  • 2020-12-11 01:36

    If I was returning two of the exact same thing, a collection might be appropriate, but in general I would usually build a specialized class to hold exactly what I needed.

    And if if you are returning two things today from those two columns, tomorrow you might want a third. Maintaining a custom object is going to be a lot easier than any of the other options.

    0 讨论(0)
  • 2020-12-11 01:37

    If your function has return value(s), it's presumably returning it/them for assignment to either a variable or an implied variable (to perform operations on, for instance.) Anything you can usefully express as a variable (or a testable value) should be fair game, and should dictate what you return.

    Your example mentions a row or a set of rows from a SQL query. Then you reasonably should be ready to deal with those as objects or arrays, which suggests an appropriate answer to your question.

    0 讨论(0)
  • 2020-12-11 01:37

    Use std::vector, QList, or some managed library container to hold however many X you want to return:

    QList<X> getMultipleItems()
    {
      QList<X> returnValue;
      for (int i = 0; i < countOfItems; ++i)
      {
        returnValue.push_back(<your data here>);
      }
      return returnValue;
    }
    
    0 讨论(0)
  • 2020-12-11 01:38

    For the situation you described, pulling two fields from a single table, the appropriate answer is #4 given that two properties (fields) of the same entity (table) will exhibit strong cohesion.

    Your concern that "it might be confusing to create a class just for the purpose of a return" is probably not that realistic. If your application is non-trivial you are likely going to need to re-use that class/object elsewhere anyway.

    0 讨论(0)
  • 2020-12-11 01:38

    I have sometimes used continuation-passing style to work around this, passing a function value as an argument, and returning that function call passing the multiple values.

    Objects in place of function values in languages without first-class functions.

    0 讨论(0)
  • 2020-12-11 01:38

    My choice is #4. Define a reference parameter in your function. That pointer references to a Value Object.

    In PHP:

    class TwoValuesVO {
      public $expectedOne;
      public $expectedTwo;
    }
    
    /* parameter $_vo references to a TwoValuesVO instance */
    function twoValues( & $_vo ) {
      $vo->expectedOne = 1;
      $vo->expectedTwo = 2;
    }
    

    In Java:

    class TwoValuesVO {
      public int expectedOne;
      public int expectedTwo;
    }
    
    class TwoValuesTest {
      void twoValues( TwoValuesVO vo ) {
        vo.expectedOne = 1;
        vo.expectedTwo = 2;
      }
    }
    
    0 讨论(0)
提交回复
热议问题