[fpc-pascal] Get highest element of a StringList

Thomas Kurz fpc.2021 at t-net.ruhr
Sat Sep 10 21:37:34 CEST 2022


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 at lists.freepascal.org>
To: 'FPC-Pascal users discussions' <fpc-pascal at 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 at lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal



More information about the fpc-pascal mailing list