Henry Olive 4 Posted August 6 Good Day, MyQuery Result is like below TotalCost...TotalCostWıthExp 100 110 Is it possible something like below function function.GetBomCosts (ItemCode:String): Value1,Value2 : Double Thank You Share this post Link to post
Serge_G 63 Posted August 6 Hi, only a single result but you can change to function GetBomCosts(const ItemCode : String; var value1, value2 : Double) : boolean begin with TFDQuery.Create do begin Value1:=0; value2:=0; Connection:= FDConnection1; SQL.Text:='SELECT itemcode,value1,value2 from table where code='+quotedstr(itemcode); active:=true; if fieldbyname('Itemcode').isNull then result:=false else begin value1:=FieldByName('value1').asFloat; value2:=FieldByName('value2').asFloat; result:=true; end; active:=false; end; end; 2 Share this post Link to post
Cristian Peța 63 Posted August 6 You can return a record: TTotalCostResult = record TotalCost: Double; TotalCostWıthExp: Double; end; function GetBomCosts(ItemCode:String): TTotalCostResult; 1 Share this post Link to post
Uwe Raabe 1360 Posted August 6 You can return a TArray<Double>: function GetBomCosts(const ItemCode : String) : TArray<Double>; begin { Retrieve values } ... { Return values as array } SetLength(Result, 2); Result[0] := value1; Reuslt[1] := value2; end; Share this post Link to post
David Heffernan 1759 Posted August 6 9 minutes ago, Uwe Raabe said: You can return a TArray<Double>: function GetBomCosts(const ItemCode : String) : TArray<Double>; begin { Retrieve values } ... { Return values as array } SetLength(Result, 2); Result[0] := value1; Reuslt[1] := value2; end; Whilst you can, this doesn't feel like a great idea. It's one thing returning a tuple in a language like Python with support for unpacking. But a Delphi dynamic array should be used for arrays, things where each item is a different value of the same thing. That's not the case here. They are two distinct things. Use a record, or two out params. Share this post Link to post
Andrea Raimondi 13 Posted August 6 9 hours ago, David Heffernan said: Whilst you can, this doesn't feel like a great idea. It's one thing returning a tuple in a language like Python with support for unpacking. But a Delphi dynamic array should be used for arrays, things where each item is a different value of the same thing. That's not the case here. They are two distinct things. Use a record, or two out params. I do not necessarily agree with this. I agree on the fact that a record would work better for several reasons and that a generic list has a number of disadvantages; however, if the function is eventually going to have multiple values, then a typed list (i.e. a generic but with a concrete type) would probably work better. However, if changes are infrequent and/or the values must be easy to identify then a record is absolutely the way to go. I would never use a dynamic array because it's too easy to mis-identify things. Share this post Link to post
David Heffernan 1759 Posted August 8 55 minutes ago, Fr0sT.Brutal said: TDoubleTuple = array [0..1] of Double That's a pair not a tuple Share this post Link to post
Stefan Glienke 1472 Posted August 8 8 hours ago, David Heffernan said: That's a pair not a tuple A pair is a tuple where n is 2 - so yes it is a tuple 1 Share this post Link to post
David Heffernan 1759 Posted August 8 1 hour ago, Stefan Glienke said: A pair is a tuple where n is 2 - so yes it is a tuple OK. What name would you give this type array [0..2] of Double Share this post Link to post
Leif Uneus 35 Posted August 8 1 hour ago, David Heffernan said: OK. What name would you give this type array [0..2] of Double Triple or triplet. See https://en.wikipedia.org/wiki/Tuple#Names_for_tuples_of_specific_lengths Share this post Link to post
David Heffernan 1759 Posted August 8 3 minutes ago, Leif Uneus said: Triple or triplet. See https://en.wikipedia.org/wiki/Tuple#Names_for_tuples_of_specific_lengths But it's a tuple too. My question was really for Stefan though. Share this post Link to post
Uwe Raabe 1360 Posted August 8 5 minutes ago, Leif Uneus said: See https://en.wikipedia.org/wiki/Tuple#Names_for_tuples_of_specific_lengths According to that list, 11 hours ago, Fr0sT.Brutal said: TDoubleTuple = array [0..1] of Double that would be better named TDouble? Share this post Link to post
Bill Meyer 306 Posted August 8 3 hours ago, Leif Uneus said: Triple or triplet. See https://en.wikipedia.org/wiki/Tuple#Names_for_tuples_of_specific_lengths The list gets a bit silly. Once you go beyond the commonly used prefixes, the information value drops like a rock. Share this post Link to post
Stefan Glienke 1472 Posted August 8 5 hours ago, David Heffernan said: OK. What name would you give this type array [0..2] of Double A static array of Double with the size 3 - exact type name would be context-specific and since you are not giving any context I cannot name it. Share this post Link to post
David Heffernan 1759 Posted August 9 3 hours ago, Stefan Glienke said: A static array of Double with the size 3 - exact type name would be context-specific and since you are not giving any context I cannot name it. We don't need any context to know that a fixed length array shouldn't be named a tuple. Share this post Link to post
Gustav Schubert 23 Posted August 9 (edited) You can return a TPointF: procedure Test; var A, B: Currency; // <-- ? P: TPointF; function GetResult(const s: string): TPointF; begin result.X := 99.98; result.Y := -1.01; end; begin { get going fast } P := GetResult('ABC'); { in order to be able to play with some data types } A := P.X; B := P.Y; end; Edited August 9 by Gustav Schubert Use of Double should be questioned before optimization. Share this post Link to post
Henry Olive 4 Posted August 9 Thank You So Much Serge, Cristian, Uwe, David, Andrea, Frost.Brutal, Stefan, Leif, Bill, Gustav Share this post Link to post
David Heffernan 1759 Posted August 9 1 hour ago, Gustav Schubert said: You can return a TPointF: function GetBomCosts(ItemCode: string): TPointF; procedure Test; var BC: TPointF; // cointains X and Y of type single TotalCost: double; TotalConstWithExp: double; begin BC := GetBomCosts('ABC'); TotalCost := BC.X; // conversion to double will be implicit TotalCostWithExp := BC.Y; end; It goes from bad to worse!! Share this post Link to post
Fr0sT.Brutal 657 Posted August 9 12 hours ago, Uwe Raabe said: that would be better named TDouble? TDoubleDouble - we really need to mark what's inside 🙂 1 Share this post Link to post
Gustav Schubert 23 Posted August 9 38 minutes ago, David Heffernan said: It goes from bad to worse!! But it started with two good answers at the top, which I liked, to make it clear. 🙂 Share this post Link to post