How do you return two values from a single method?

后端 未结 22 852
谎友^
谎友^ 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条回答
  •  旧时难觅i
    2020-12-11 01:59

    Use var/out parameters or pass variables by reference, not by value. In Delphi:

    function ReturnTwoValues(out Param1: Integer):Integer;
    begin
      Param1 := 10;
      Result := 20;
    end;
    

    If you use var instead of out, you can pre-initialize the parameter.

    With databases, you could have an out parameter per column and the result of the function would be a boolean indicating if the record is retrieved correctly or not. (Although I would use a single record class to hold the column values.)

提交回复
热议问题