Forum > General

How does MaxValue() work on Records and Multidimensional Arrays ?

(1/3) > >>

FrankBKK:
I created a record and tried to get the Min/Max values for some of those columns via Math lib's MaxValue() but that does not seem to work.

Am i missing something syntax-wise or is there another way besides looping through the whole Array ?


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---type  TTest = Record    Name : String ;    ValuesA  : Integer ;    ValuesB : Integer ;  end ;  aTest = array of TTest;    // both of them give compiler errors:iMax := MaxValue(aTest.ValuesA) ; iMin := aTest.ValuesA.MinValue ;   





avk:
The Math.MaxValue()/MinValue() functions only work with arrays of simple numeric types. Personally, I know of only one third-party library that "out of the box" provides the functions you need for user-defined types - LGenerics.

FrankBKK:
Thanks for your prompt feedback.

Yeah, it seems this way - hard to find any info on this ....

I will have a look on LGenerics - looks promising.

wp:
The MaxValue function is not applicable to this case; as avk already noted the array elements must be simple numeric data types, but in your case the array constists of records, and the maxValue should be applied to one of the record elements.

But you can easily work around by quickly writing a record helper:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program Project1; {$MODE delphi} type  TTest = Record    Name : String ;    ValuesA  : Integer ;    ValuesB : Integer ;  end ;  TTestArray = array of TTest;   TTestArrayHelper = record helper for TTestArray    function MaxA: Integer;    function MaxB: Integer;  end;   function TTestArrayHelper.MaxA: Integer;  var    t: TTest;  begin    Result := -MaxInt;    for t in self do      if t.ValuesA > Result then Result := t.ValuesA;  end;   function TTestArrayHelper.MaxB: Integer;  var    t: TTest;  begin    Result := -MaxInt;    for t in self do      if t.ValuesB > Result then Result := t.ValuesB;  end; var  test: TTestArray;begin  SetLength(test, 2);   test[0].Name := 'A';  test[0].ValuesA := 11;  test[0].ValuesB := 2;   test[1].Name := 'B';  test[1].ValuesA := 10;  test[1].ValuesB := 20;   WriteLn(test.MaxA);  WriteLn(test.MaxB);   ReadLn; end.

FrankBKK:
Perfect, exactly what I wanted  !

Thanks a lot for your help !

Navigation

[0] Message Index

[#] Next page

Go to full version