Script for property getter/setter

Please ignore the last question. Through some trial and error I managed to solve the issue. I answer it here to create an example that may of use for someone else in the future.

I first create the getter and setter:

procedure OnClassGenerated(Args: TClassGeneratedArgs);
var
  Func: TCodeMemberMethod; 
  Proc: TCodeMemberMethod;
begin
   Func := Args.CodeType.AddFunction('GetAmount', 'Double', mvPrivate);                       
   Func.AddSnippet('if FCategory = 0 then');  
   Func.AddSnippet('   result := FAmount');
   Func.AddSnippet('else');
   Func.AddSnippet('   result := FAmount * -1;'); 
        
    Proc := Args.CodeType.AddProcedure('SetAmount', mvPrivate);
    Proc.AddParameter('Value', 'Double').Modifier := pmNone
    Proc.AddSnippet('if FCategory = 0 then');  
    Proc.AddSnippet('   FAmount := Value');
    Proc.AddSnippet('else');
    Proc.AddSnippet('   FAmount := Value * -1;');            
end;                                              

Note the .Modifier := pmNone, pmVar is not allowed for property setter

I then modify the property line in the class as:

procedure OnColumnGenerated(Args: TColumnGeneratedArgs);
begin
   if Args.CodeType.Name = 'TMyEntity' then
   begin
      if Args.Prop.Name = 'Amount' then
      begin        
         Args.Prop.ReadMember := 'GetAmount';    
         Args.Prop.WriteMember := 'SetAmount'; 
      end;
   end;
end;

This results in the desired code:

Property Amount: Double read GetAmount write SetAmount;

Note the Args.CodeType.Name = 'TMyEntity'!, otherwise property 'Amount' will be changed for all classes! Which happened to me.... ;-)

1 Like