I want to do something like this:
type
TSession = class
private
function GetName: String; // basically Exit (FName)
procedure SetName (AName: String) // basically FName := AName
FName: String;
public
property Name: String read GetName write SetName;
end;
type
TMain = class
private
function GetSession (AID: Integer): TSession;
function GetSessionName (AID: Integer): String;
procedure SetSessionName (AID: Integer; AName: String);
FLock: TCriticalSection;
FSessions: specialize TDictionary<Integer,TSession>;
public
public SessionName[ID: Integer]: String read GetSessionName write SetSessionName;
end;
function TMain.GetSession (AID: Integer): TSession;
begin
FLock.Enter;
try
Exit (FLock.Items[AID]);
finally
FLock.Leave;
end;
end;
function TMain.GetSessionName (AID: Integer): String;
var s: TSession;
begin
s := GetSession(AID);
if Assigned(s) then Exit (s.Name) else exit ('');
end;
procedure TMain.SetSessionName (AID: Integer; AName: String);
var s: TSession;
begin
s := GetSession(AID);
if Assigned(s) then s.Name := AName;
end;
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:
...
function GetSessionString (AIndex: Integer; AID: Integer): String;
procedure SetSessionName (AIndex: Integer; AID: Integer; AName:
public SessionName[ID: Integer]: String index 1 read GetSessionString write SetSessionString; // illegal symbol for property access
...