delphigetter-setterautocreate

How to auto-generate getter and setter methods in Delphi?


I'm a Java developer and I always use the getter-setter methods.
How can I use this concept in Delphi?

for this example:

unit Unit1;

type
  ClassePippo=class
  private
    colorText:string; //1
    function getColorText: String; //3
    procedure setColorText(const Value: String); //3
  public
    property colore: String read getColorText write setColorText;  //2
  end;

implementation

{ ClassePippo }

function ClassePippo.getColorText: String; //3
begin
  Result:=colorText;
end;

procedure ClassePippo.setColorText(const Value: String); //3
begin
  colorText:=Value;
end;

end.

Is there a feature to auto-create the getter and setter methods?

I only want to write colorText: string; //1 and invoke a shortcut and I want that the IDE auto-creates //2 and //3.

(When I develop in Java using Eclipse I can auto-generate the getter and setter methods using Source-->Generate getter and setter...)


Solution

  • Type out the property you want first rather than the internal variable. Just create type the following in your class So

    Property Colore : String Read GetColorText Write SetColorText;

    then press Ctrl Shift C

    the IDE will then create the getter, the setter and the private internal variable.

    Note that Property setters and getters are optional in Object Pascal. You can just as easily write

    Property Colore : String Read FColorText Write FColorText;

    or have just a setter or getter

    Property Colore : String Read FColorText Write SetColorText;

    In this case the IDE will generate the private FColorText variable and a setter method SetColorText