But, if that (eventually) leads to the highest one, and that's all that's desired, why not just use the whole expression variable := pred(f.Count) instead of the whole loop?

Wouldn't that accomplish the same thing?

Why the loop?


On 9/10/2022 3:44 PM, Thomas Kurz via fpc-pascal wrote:
If you just don't like the "-1" for readability, you might also wan't to 
consider using

for i := 0 to Pred(f.Count) do ...


----- Original Message -----
From: Thomas Kurz via fpc-pascal <fpc-pascal@lists.freepascal.org>
To: 'FPC-Pascal users discussions' <fpc-pascal@lists.freepascal.org>
Sent: Saturday, September 10, 2022, 21:37:34
Subject: [fpc-pascal] Get highest element of a StringList

Try this (note the "modeswitch"):

program Project1;

{$modeswitch typehelpers}

uses
   Classes,
   SysUtils;

type TStringListHelper = class helper for TStringList
   function High: NativeInt;
end;

function TStringListHelper.High: NativeInt;
begin
   Exit (Self.Count-1);
end;

var
   f: TStringList = nil;
   i: Integer;

begin
   f := TStringList.Create;
   f.Add ('hallo');
   f.Add ('welt');
   f.Add ('!');
   for i := 0 to f.High do Writeln (f[i]);
   FreeAndNil (f);
end.





----- Original Message -----
From: James Richters via fpc-pascal <fpc-pascal@lists.freepascal.org>
To: 'FPC-Pascal users discussions' <fpc-pascal@lists.freepascal.org>
Sent: Saturday, September 10, 2022, 19:57:51
Subject: [fpc-pascal] Get highest element of a StringList

This Helper sounds like a nice way to do it.  I need the numerical index of
the loop so this would be more straight forward.
Would I then do something like :
For I:= 0 to MyStringList.High Do
?

When I try to add the Type statement:
type TStringListHelper = class helper for TStringList function High:
NativeInt; end;
I get    Error: Identifier not found "class"

I have the Classes unit.  Is there something else I am missing?

James

Another alternative would be declaring a helper:
type TStringListHelper = class helper for TStringList function High:
NativeInt; end;

function TStringListHelper.High: NativeInt; begin
  Exit (Self.Count-1);
end;
_______________________________________________
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

_______________________________________________
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

_______________________________________________
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
_______________________________________________
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Reply via email to