Recent

Author Topic: [solved] is there a shortcut for accessing child properties?  (Read 1138 times)

piola

  • Full Member
  • ***
  • Posts: 118
  • Lazarus 2.2, 64bit on Windows 8.1 x64
[solved] is there a shortcut for accessing child properties?
« on: September 18, 2021, 11:09:02 am »
I want to do something like this:

Code: Pascal  [Select][+][-]
  1. type
  2.   TSession = class
  3.     private
  4.       function GetName: String; // basically Exit (FName)
  5.       procedure SetName (AName: String) // basically FName := AName
  6.       FName: String;
  7.     public
  8.       property Name: String read GetName write SetName;
  9.  end;
  10.  
  11. type
  12.   TMain = class
  13.     private
  14.       function GetSession (AID: Integer): TSession;
  15.       function GetSessionName (AID: Integer): String;
  16.       procedure SetSessionName (AID: Integer; AName: String);
  17.       FLock: TCriticalSection;
  18.       FSessions: specialize TDictionary<Integer,TSession>;
  19.     public
  20.       public SessionName[ID: Integer]: String read GetSessionName write SetSessionName;
  21.   end;
  22.  
  23. function TMain.GetSession (AID: Integer): TSession;
  24.   begin
  25.     FLock.Enter;
  26.     try
  27.       Exit (FLock.Items[AID]);
  28.     finally
  29.       FLock.Leave;
  30.     end;
  31.   end;
  32.  
  33. function TMain.GetSessionName (AID: Integer): String;
  34.   var s: TSession;
  35.   begin
  36.     s := GetSession(AID);
  37.     if Assigned(s) then Exit (s.Name) else exit ('');
  38.   end;
  39.  
  40. procedure TMain.SetSessionName (AID: Integer; AName: String);
  41.   var s: TSession;
  42.   begin
  43.     s := GetSession(AID);
  44.     if Assigned(s) then s.Name := AName;
  45.   end;
  46.  

I will have several dozens of properties with only a few distinct types (int, string, boolean).

Is there a way to handle all properties with the same type with common getters and setters or do I really have to write seperate getters and setters for each property? (They will, of course, vary only in the property name that is passed to the TSession object.)

Edit:

I already tried a common indexed property to handle all string-typed properties, but FPC seems not to accept a mixture of indexed and array property:

Code: Pascal  [Select][+][-]
  1. ...
  2.       function GetSessionString (AIndex: Integer; AID: Integer): String;
  3.       procedure SetSessionName (AIndex: Integer; AID: Integer; AName:
  4.       public SessionName[ID: Integer]: String index 1 read GetSessionString write SetSessionString; // illegal symbol for property access
  5. ...
  6.  
« Last Edit: September 18, 2021, 12:36:33 pm by piola »

ASerge

  • Hero Member
  • *****
  • Posts: 2212
Re: is there a shortcut for accessing child properties?
« Reply #1 on: September 18, 2021, 12:17:22 pm »
I already tried a common indexed property to handle all string-typed properties, but FPC seems not to accept a mixture of indexed and array property:
It supports it normally. You have provided some pieces of unrelated code. Show the normal one, and where the error is. Don't forget that for a read function, the Index must be the last parameter and for a write procedure, it must be the second-to-last parameter.

piola

  • Full Member
  • ***
  • Posts: 118
  • Lazarus 2.2, 64bit on Windows 8.1 x64
Re: is there a shortcut for accessing child properties?
« Reply #2 on: September 18, 2021, 12:36:21 pm »
Ok, I had a typo in there. So according to your reply it should be:

Code: Pascal  [Select][+][-]
  1. ...
  2. function GetSessionString (AID: Integer; AIndex: Integer): String;
  3. procedure SetSessionString (AID: Integer; AIndex: Integer; AName:
  4. public SessionName[ID: Integer]: String index 1 read GetSessionString write SetSessionString; // illegal symbol for property access
  5. ...
  6.  

That works. Thank you!

 

TinyPortal © 2005-2018