Web forum is in read-only mode. Login as active registered customer for write access
  Forum Search   New Posts New Posts

Creating constructors with paramenters

 Post Reply Post Reply
Author
Alex Egorov View Drop Down
Member
Member
Avatar

Joined: 04 May 2017
Posts: 53
Post Options Post Options   Quote Alex Egorov Quote  Post ReplyReply Direct Link To This Post Topic: Creating constructors with paramenters
    Posted: 04 Nov 2019 at 11:13am
Hello, is it possible to use scripting to create the second constructor with parameters?Something like this:
procedure OnClassGenerated(Args: TClassGeneratedArgs);
var
  Proc: TCodeMemberMethod;    
begin
  case Args.CodeType.Name of
    'TAcc': begin  
      Proc := Args.CodeType.AddConstructor('CreateUser', mvPublic);
      Proc.AddParameter('Name', 'string').Modifier := pmConst;  
      Proc.AddSnippet('Result := inherited;');
      Proc.AddSnippet('Result.Name := Name');
    end; 
  end;
end;
to get this second constructor:
constructor TAcc.CreateUser(const Name: string);
begin
  Result := inherited;
  Result.Name := Name
end;
Back to Top
Wagner R. Landgraf View Drop Down
TMS Support
TMS Support
Avatar

Joined: 18 May 2010
Posts: 2552
Post Options Post Options   Quote Wagner R. Landgraf Quote  Post ReplyReply Direct Link To This Post Posted: 05 Nov 2019 at 3:48pm
Yes, you can use something like this:

procedure OnClassGenerated(Args: TClassGeneratedArgs);     
var                                         
  Proc: TCodeMemberConstructor;        
begin                                   
  case Args.CodeType.Name of
    'TCategories':
      begin
        Proc := TCodeMemberConstructor.Create;
        Args.CodeType.Members.Add(Proc);             
        Proc.Name := 'Create';
        Proc.Directives := SetOf([mdOverload]);
        Proc.Visibility := mvPublic;
        Proc.AddParameter('Name', 'string').Modifier := pmConst;  
        Proc.AddSnippet('inherited Create;');
        Proc.AddSnippet('Self.Name := Name;');
      end;
  end;                                                   
end;
Back to Top
Alex Egorov View Drop Down
Member
Member
Avatar

Joined: 04 May 2017
Posts: 53
Post Options Post Options   Quote Alex Egorov Quote  Post ReplyReply Direct Link To This Post Posted: 05 Nov 2019 at 9:45pm
Thank you very much. How I can add "overload" for the standard constructor in this situation?
Back to Top
Alex Egorov View Drop Down
Member
Member
Avatar

Joined: 04 May 2017
Posts: 53
Post Options Post Options   Quote Alex Egorov Quote  Post ReplyReply Direct Link To This Post Posted: 05 Nov 2019 at 11:31pm
Now I'm use this method to add "overload" for the standard constructor, may be it is not optimal?
procedure OnClassGenerated(Args: TClassGeneratedArgs);
var       
  I: Integer;
  Member: TCodeTypeMember;
  Proc1, Proc2: TCodeMemberConstructor;        
begin                                   
  case Args.CodeType.Name of
    'TAcc': 
      begin 
        for I := 0 to Args.CodeType.Members.Count - 1 do begin
          Member := Args.CodeType.Members[ I ];
          if Member.Name = 'Create' then begin 
            Proc1 := Member;
            Proc1.Directives := Proc1.Directives + SetOf([mdOverload]);
            Break;
          end;
        end;  
        
        Proc2 := TCodeMemberConstructor.Create;
        Args.CodeType.Members.Add(Proc2);             
        Proc2.Name := 'Create';
        Proc2.Directives := SetOf([mdOverload]);
        Proc2.Visibility := mvPublic;
        Proc2.AddParameter('Name', 'string').Modifier := pmConst;  
        Proc2.AddSnippet('Create;');
        Proc2.AddSnippet('Self.Name := Name;');
      end;       
  end;            
end;
Back to Top
Wagner R. Landgraf View Drop Down
TMS Support
TMS Support
Avatar

Joined: 18 May 2010
Posts: 2552
Post Options Post Options   Quote Wagner R. Landgraf Quote  Post ReplyReply Direct Link To This Post Posted: 06 Nov 2019 at 12:18pm
It's a good approach.
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down