Recent

Author Topic: Type helper for TStringArray or Array  (Read 513 times)

lainz

  • Hero Member
  • *****
  • Posts: 4044
  • Leandro Diaz
Type helper for TStringArray or Array
« on: March 24, 2021, 02:35:31 pm »
Code: Pascal  [Select][+][-]
  1.    TStringArray = Type Helper for TStringArray
  2.   private
  3.     function GetLength: Integer;
  4.     procedure SetLength(AValue: Integer);
  5.   public
  6.     property Length: Integer read GetLength write SetLength;
  7.   end;
  8.  
  9.  
  10. function TStringArray.GetLength: Integer;
  11. begin
  12.   Result := Length(Self);
  13. end;
  14.  
  15. procedure TStringArray.SetLength(AValue: Integer);
  16. begin
  17.   SetLength(Self, AValue);
  18. end;  
  19.  
  20.  

Code: Pascal  [Select][+][-]
  1. var
  2.   arr: TStringArray;
  3. begin
  4.   arr.length := 10;
  5.   ShowMessage(arr.Length.ToString);

Why this doesn't compile on latest release of Lazarus?

How to implement it?


avk

  • Sr. Member
  • ****
  • Posts: 411
    • my self-education project
Re: Type helper for TStringArray or Array
« Reply #1 on: March 24, 2021, 02:49:19 pm »
This works for me:
Code: Pascal  [Select][+][-]
  1. ...
  2. {$modeswitch typehelpers}
  3. ...
  4. type
  5.   TStringArrayHelper = type Helper for TStringArray
  6.   private
  7.     function GetLength: Integer;
  8.     procedure SetLength(AValue: Integer);
  9.   public
  10.     property Length: Integer read GetLength write SetLength;
  11.   end;
  12.  
  13.  
  14. function TStringArrayHelper.GetLength: Integer;
  15. begin
  16.   Result := System.Length(Self);
  17. end;
  18.  
  19. procedure TStringArrayHelper.SetLength(AValue: Integer);
  20. begin
  21.   System.SetLength(Self, AValue);
  22. end;
  23.  

vladimirr

  • New Member
  • *
  • Posts: 25
Re: Type helper for TStringArray or Array
« Reply #2 on: March 24, 2021, 03:05:49 pm »
My version:

Code: Pascal  [Select][+][-]
  1. type
  2.   TStringArrayHelper = type helper for TStringArray
  3.   private
  4.     function GetCount: integer;
  5.     procedure  SetCount(Value: integer);
  6.   public
  7.     function IsEmpty: Boolean;
  8.     procedure Add(const Value: string);
  9.     property Count: integer read GetCount write SetCount;
  10.   end;
  11.  

Code: Pascal  [Select][+][-]
  1. function TStringArrayHelper.IsEmpty: Boolean;
  2. begin
  3.   Result := Length(Self) = 0;
  4. end;
  5.  
  6. procedure TStringArrayHelper.SetCount(Value: integer);
  7. begin
  8.   SetLength(Self, Value);
  9. end;
  10.  
  11. function TStringArrayHelper.GetCount: integer;
  12. begin
  13.   Result := Length(Self);
  14. end;
  15.  
  16. procedure TStringArrayHelper.Add(const Value: string);
  17. begin
  18.   SetLength(Self, Length(Self)+1);
  19.   Self[Length(Self)-1] := Value;
  20. end;
  21.  

Count like in TStringList.

lainz

  • Hero Member
  • *****
  • Posts: 4044
  • Leandro Diaz
Re: Type helper for TStringArray or Array
« Reply #3 on: March 24, 2021, 08:39:31 pm »
Thanks.  :)

 

TinyPortal © 2005-2018