Lazarus

Free Pascal => General => Topic started by: egsuh on September 26, 2021, 07:26:16 am

Title: Which procedure should I override?
Post by: egsuh on September 26, 2021, 07:26:16 am
I'd like to define a TStringList descendant so that each string adds objects automatically. I.e., I'd like to have the same effect as porcedure LoadPairs below, just with MyStringList.Text:= s;.  In the real program, I'll add other fields to TMyObject.

Code: Pascal  [Select][+][-]
  1. type
  2.   TMyObject=class
  3.         Name,
  4.         Value: string;
  5.   end;
  6.  
  7.   TMyStringList = class(TStringList)
  8.  
  9.  
  10.        procedure LoadPairs(s: string);
  11.    end;
  12.  
  13. implementation
  14.  
  15. procedure TMyStringList.LoadPairs(s: string);
  16. var
  17.      i: integer;
  18.      AnObject:
  19. begin
  20.      Text:= s;
  21.      for i:= 0 to count-1 do begin
  22.           AnObject:= TMyObject.Create;
  23.           AnObject.Name:= Names[i];
  24.           AnObject.Value:= ValueFromIndex[i];
  25.           Objects[i]:= AnObject;
  26.     end;
  27. end;  
  28.  


There is:   
 public procedure TStrings.SetText( TheText: PChar); virtual
   
The parameter is PChar type.


Title: Re: Which procedure should I override?
Post by: Handoko on September 26, 2021, 07:40:18 am
Maybe:

Code: Pascal  [Select][+][-]
  1. type
  2.  
  3.   TMyObject=class
  4.     Name,
  5.     Value: string;
  6.   end;
  7.  
  8.   TMyStringList = class(TStringList)
  9.     procedure LoadPairs(S: string);
  10.   protected
  11.     procedure SetTextStr(const Value: string); override;
  12.   end;
  13.  
  14. implementation
  15.  
  16. procedure TMyStringList.LoadPairs(S: string);
  17. var
  18.   AnObject: TMyObject;
  19.   i:        Integer;
  20. begin
  21. //  Text := S;
  22.   for i := 0 to Count-1 do begin
  23.     AnObject       := TMyObject.Create;
  24.     AnObject.Name  := Names[i];
  25.     AnObject.Value := ValueFromIndex[i];
  26.     Objects[i]:= AnObject;
  27.   end;
  28. end;
  29.  
  30. procedure TMyStringList.SetTextStr(const Value: string);
  31. begin
  32.   inherited SetTextStr(Value);
  33.   LoadPairs(Value);
  34. end;

The code above produces no compile-time error nor warning. But because you create some instances of TMyObject at runtime, you have to think how to manage and free them correctly.

Edit:
Line #21 should be replaced with line #32.
Title: Re: Which procedure should I override?
Post by: egsuh on September 26, 2021, 08:03:22 am
Thank you, Handoko.  Your codes compiles well.

In practice I can't use this, because I have to add other properties. There should be some more codes. I'm currently designing object which was just a text string so far. My purpose here is to test the outside operations --- just adding objects to TMyStringList.
I'll test the TStrings.OwnsObjects property at the same time. 
Title: Re: Which procedure should I override?
Post by: skalogryz on September 26, 2021, 08:13:02 am
I'd like to define a TStringList descendant so that each string adds objects automatically.
you need to override InsertItem() of TStringList in the following manner:
Code: Pascal  [Select][+][-]
  1. procedure TMyStringList.InsertItem(Index: Integer; const S: string; O: TObject);
  2. var
  3.   isover: boolean;
  4. begin
  5.   isover := (O = nil);
  6.   if isover  then O:=TMyObject.Create;
  7.  
  8.   inherited InsertItem(Index, S, O);
  9.  
  10.   if (isover) then begin
  11.     TMyObject(o).Name :=  Names[i];
  12.     TMyObject(o).Value := ValueFromIndex[i];
  13.   end;
  14. end;
  15.  
Title: Re: Which procedure should I override?
Post by: egsuh on September 26, 2021, 08:35:11 am
I see. Thank you very much !!!!
TinyPortal © 2005-2018