Recent

Author Topic: TStringList with .Count method  (Read 956 times)

Curt Carpenter

  • Hero Member
  • *****
  • Posts: 605
TStringList with .Count method
« on: February 16, 2025, 08:58:43 pm »
Is there a TStringList or TStrings - decendant class that implements the "count" method somewhere?   "Count" is a TStrings property but apparently not implemented.

BrunoK

  • Hero Member
  • *****
  • Posts: 683
  • Retired programmer
Re: TStringList with .Count method
« Reply #1 on: February 16, 2025, 09:19:42 pm »
AFAIK TStringList implements Count property.

TRon

  • Hero Member
  • *****
  • Posts: 4148
Re: TStringList with .Count method
« Reply #2 on: February 16, 2025, 09:45:25 pm »
... "Count" is a TStrings property but apparently not implemented.
Out of curiosity, whatever gave you that idea ?

Look at the documentation for tstringlist. Can you see that its ancestor is TStrings ? When you click that ancestor you can also notice that count is a public property. Guess what happens when you inherit a class from tstrings -> all these properties come to live (unless explicitly hidden).
Today is tomorrow's yesterday.

Curt Carpenter

  • Hero Member
  • *****
  • Posts: 605
Re: TStringList with .Count method
« Reply #3 on: February 16, 2025, 10:44:47 pm »
Out of curiosity, whatever gave you that idea ?
Look at the documentation for tstringlist. Can you see that its ancestor is TStrings ? When you click that ancestor you can also notice that count is a public property. Guess what happens when you inherit a class from tstrings -> all these properties come to live (unless explicitly hidden).

I guess whatever gave me that idea is the fact that count property would not work in my simple fpc program using TStringList or TStrings, and from this item from "LHelp - (RTL) RunTime Library"   for TStrings:

Quote
Description

Count is the current number of strings in the list. TStrings does not implement this property; descendant classes should override the property read handler to return the correct value.

Perhaps I am using an outdated help, or perhaps I don't understand what "TStrings does not implement this property" means.  You tell me.

TRon

  • Hero Member
  • *****
  • Posts: 4148
Re: TStringList with .Count method
« Reply #4 on: February 16, 2025, 11:01:06 pm »
I guess whatever gave me that idea is the fact that count property would not work in my simple fpc program using TStringList or TStrings, and from this item from "LHelp - (RTL) RunTime Library"   for TStrings:
That is weird, this works for me:
Code: Pascal  [Select][+][-]
  1. program test;
  2.  
  3. {$mode objfpc}{$h+}
  4. uses
  5.   classes, sysutils;
  6. var
  7.   sl : TStringList;
  8.   n  : integer;
  9. begin
  10.   sl := TStringList.Create;
  11.  
  12.   for n := 1 to 100
  13.     do sl.Add(n.ToString);
  14.  
  15.   writeln(sl.count);
  16.  
  17.   for n := 1 to 10 do
  18.   begin
  19.     sl.pop;
  20.     writeln(sl.count);
  21.   end;
  22.  
  23.   sl.Free;
  24. end.
  25.  


Quote
Perhaps I am using an outdated help, or perhaps I don't understand what "TStrings does not implement this property" means.  You tell me.

I'll underline the important part.
Quote
Description

Count is the current number of strings in the list. TStrings does not implement this property; descendant classes should override the property read handler to return the correct value.

Which in layman's terms translates to: the property is there but if you inherit from TStrings the descendant class must override the count property getter. This is what TStringList actually does:

Code: Pascal  [Select][+][-]
  1. TStringList = class(TStrings)
  2.   private
  3.     FList: PStringItemList;
  4.     FCount: Integer;
  5.     FCapacity: Integer;
  6.     FOnChange: TNotifyEvent;
  7.     FOnChanging: TNotifyEvent;
  8.     FDuplicates: TDuplicates;
  9.     FCaseSensitive : Boolean;
  10.     FForceSort : Boolean;
  11.     FOwnsObjects : Boolean;
  12.     FSortStyle: TStringsSortStyle;
  13.     procedure ExchangeItemsInt(Index1, Index2: Integer); inline;
  14.     function GetSorted: Boolean;
  15.     procedure Grow;
  16.     procedure InternalClear(FromIndex : Integer = 0; ClearOnly : Boolean = False);
  17.     procedure QuickSort(L, R: Integer; CompareFn: TStringListSortCompare);
  18.     procedure SetSorted(Value: Boolean);
  19.     procedure SetCaseSensitive(b : boolean);
  20.     procedure SetSortStyle(AValue: TStringsSortStyle);
  21.   protected
  22.     Procedure CheckIndex(AIndex : Integer); inline;
  23.     procedure ExchangeItems(Index1, Index2: Integer); virtual;
  24.     procedure Changed; virtual;
  25.     procedure Changing; virtual;
  26.     function Get(Index: Integer): string; override;
  27.     function GetCapacity: Integer; override;
  28.     function GetCount: Integer; override;      < ============================= property count getter is overriden.
  29.     function GetObject(Index: Integer): TObject; override;
  30.     procedure Put(Index: Integer; const S: string); override;
  31.     procedure PutObject(Index: Integer; AObject: TObject); override;
  32.     procedure SetCapacity(NewCapacity: Integer); override;
  33.     procedure SetUpdateState(Updating: Boolean); override;
  34.     procedure InsertItem(Index: Integer; const S: string); virtual;
  35.     procedure InsertItem(Index: Integer; const S: string; O: TObject); virtual;
  36.     Function DoCompareText(const s1,s2 : string) : PtrInt; override;
  37.  
  38.   public
  39.     destructor Destroy; override;
  40.     function Add(const S: string): Integer; override;
  41.     procedure Clear; override;
  42.     procedure Delete(Index: Integer); override;
  43.     procedure Exchange(Index1, Index2: Integer); override;
  44.     function Find(const S: string; Out Index: Integer): Boolean; virtual;
  45.     function IndexOf(const S: string): Integer; override;
  46.     procedure Insert(Index: Integer; const S: string); override;
  47.     procedure Sort; virtual;
  48.     procedure CustomSort(CompareFn: TStringListSortCompare); virtual;
  49.     property Duplicates: TDuplicates read FDuplicates write FDuplicates;
  50.     property Sorted: Boolean read GetSorted write SetSorted;
  51.     property CaseSensitive: Boolean read FCaseSensitive write SetCaseSensitive;
  52.     property OnChange: TNotifyEvent read FOnChange write FOnChange;
  53.     property OnChanging: TNotifyEvent read FOnChanging write FOnChanging;
  54.     property OwnsObjects : boolean read FOwnsObjects write FOwnsObjects;
  55.     Property SortStyle : TStringsSortStyle Read FSortStyle Write SetSortStyle;
  56.   end;
  57.  

PS and fwiw: the description is not accurate and misleading.
« Last Edit: February 16, 2025, 11:13:37 pm by TRon »
Today is tomorrow's yesterday.

dsiders

  • Hero Member
  • *****
  • Posts: 1377
Re: TStringList with .Count method
« Reply #5 on: February 17, 2025, 12:34:10 am »
Perhaps I am using an outdated help, or perhaps I don't understand what "TStrings does not implement this property" means.  You tell me.

It means that TStrings does not implement the property... but TStringList (a descendant) does.
Preview the next Lazarus documentation release at: https://dsiders.gitlab.io/lazdocsnext

Khrys

  • Full Member
  • ***
  • Posts: 177
Re: TStringList with .Count method
« Reply #6 on: February 17, 2025, 07:29:52 am »
Perhaps I am using an outdated help, or perhaps I don't understand what "TStrings does not implement this property" means.  You tell me.

PS and fwiw: the description is not accurate and misleading.

The  Count  property is backed by a member function that is both  virtual  and  abstract,  and as such it is literally not present in the  implementation  section. So although the description may be misleading, it certainly is accurate in my opinion  :):

Code: Pascal  [Select][+][-]
  1. unit Example_TStrings;
  2.  
  3. interface
  4.  
  5. type
  6.   TStrings = class
  7.   protected
  8.     function GetCount(): Integer; virtual; abstract;
  9.   public
  10.     constructor Create();
  11.     property Count: Integer read GetCount;
  12.   end;
  13.  
  14. implementation
  15.  
  16. // function TStrings.GetCount(): Integer;
  17. // ^ not present
  18.  
  19. constructor TStrings.Create();
  20. begin
  21.   inherited Create();
  22. end;
  23.  
  24. end.

d2010

  • Full Member
  • ***
  • Posts: 118
Re: TStringList with .Count method
« Reply #7 on: February 17, 2025, 07:51:08 am »
Is there a TStringList or TStrings - decendant class that implements the "count" method somewhere?   "Count" is a TStrings property but apparently not implemented.

Code: [Select]
Type TArsenieBo=record  maxstr,minstr,cntstr:integer;End;
Function TString_fake_count(var des:TStringList):integer;
Var cnt:integer;mpr:pchar;
Begin  cnt:=000;
    if (des=nil) then exit(000);
    mpr:=des.GetText;
    if (mpr=nil) then exit(-100);
     writeln('Here you count-of-Lines');
   while (mpr>#0) do begin if (mpr^=#10) then inc(cnt); inc(mpr);end;
   result:=cnt;
End;
The main key is at ??? at code-bellow

Code: [Select]
Function TString_fake_maxcount(var des:TStringList):TArsenieBo;
Var dwg,irc,cnt:integer;old,mpr:pointer; amin,amax:integer;
Begin  cnt:=000; irc:=000;
    amin:=MAXINT;
    aMax:=-500;
    dwg:=000;
    result.minstr:=0000;
     irc:=000;
     result.maxstr:=000;   result.cntstr=000; if (des=nil) then exit;
    mpr:=des.GetText; ???
    if (mpr=nil) then begin result.cntstr:=-100;result.maxstr:=-100;exit;end;
     writeln('Here you count-of-Lines & and get MaxOf Length Line ');
   result.cntstr:=00;
   result.maxstr:=000;
   old:=mpr;
   while (mpr>#0) do
     begin
        if (mpr^=#10) then begin inc(result.cntstr); ???
            irc:=000;
        while (mpr^ in [#13,#10]) do begin inc(mpr); inc(irc);end;
         if (irc>002) then writeln('Your Tstring have too many emtpy Lines inside-> Perhasp is broken code');
                                    dwg:=Cardinal(mpr)-Cardinal(old)+01;                         
           if (dwg>aMax) then aMax:=aMin;
          if (dwg<aMin) then aMIn:=dwg;
          old:=mpr+ sizeof(char)+sizeof(char);
               End; {if}
        mpr:=mpr+sizeof(char);
  End;{WHILE}
  result.maxstr:=aMax;
  result.minstr:=aMin;
End;
« Last Edit: February 17, 2025, 07:59:50 am by d2010 »

Thaddy

  • Hero Member
  • *****
  • Posts: 16653
  • Kallstadt seems a good place to evict Trump to.
Re: TStringList with .Count method
« Reply #8 on: February 17, 2025, 08:12:33 am »
Curt, you really should learn to examine the documentation first and all the others should learn to refer to the documentation first.
Then the thread could be kept to a single answer:
https://www.freepascal.org/docs-html/rtl/classes/tstrings.count.html

Read The Fomal Manual.
But I am sure they don't want the Trumps back...

TRon

  • Hero Member
  • *****
  • Posts: 4148
Re: TStringList with .Count method
« Reply #9 on: February 17, 2025, 08:44:44 am »
The  Count  property is backed by a member function that is both  virtual  and  abstract,  and as such it is literally not present in the  implementation  section. So although the description may be misleading, it certainly is accurate in my opinion  :):
Accurate in the sense that it results to that, yes. Imho the Delphi description more clear in that regards:
Quote
Introduces an abstract property to represent the number of strings in the list.
Today is tomorrow's yesterday.

Thaddy

  • Hero Member
  • *****
  • Posts: 16653
  • Kallstadt seems a good place to evict Trump to.
Re: TStringList with .Count method
« Reply #10 on: February 17, 2025, 09:01:53 am »
@TRon
You can't read manuals either?
Shock, horror.  :-X
But I am sure they don't want the Trumps back...

TRon

  • Hero Member
  • *****
  • Posts: 4148
Re: TStringList with .Count method
« Reply #11 on: February 17, 2025, 09:23:17 am »
Literally my first response to OP Thaddy, in case you missed it.
Today is tomorrow's yesterday.

Curt Carpenter

  • Hero Member
  • *****
  • Posts: 605
Re: TStringList with .Count method
« Reply #12 on: February 21, 2025, 01:31:29 am »
Sorry to be so late to return to this.  I was working on a deadline and had to leave it for another day.

My error, I discovered, was the result of using both TStrings and TStringList in my application.  Use of the count property was well removed from the declarations, and at some point and for some reason I concluded that it wasn't working at all since it wasn't working in the case of the TStrings usage. To get on with things I stopped using both and implemented my own lists.

Interestingly my confusion was in part due to my misreading of the documentation -- a note for the "RTFM" crowd.  Anyway, lessons learned.

n7800

  • Sr. Member
  • ****
  • Posts: 257
Re: TStringList with .Count method
« Reply #13 on: February 22, 2025, 09:48:54 pm »
Curt, TStrings is just a base class that is a "layout" for its child classes. Each of these classes can be used for different purposes, and implement its properties in its own way.

Actually, forget it - it's just an implementation detail of TStringList, which is almost always used to work with lists of strings.

Curt Carpenter

  • Hero Member
  • *****
  • Posts: 605
Re: TStringList with .Count method
« Reply #14 on: February 22, 2025, 10:57:31 pm »
Thanks. Got it -- I understand now.

 

TinyPortal © 2005-2018