8

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

  • I define a local variable //1
  • I create a property //2
  • I press CTRL+SHIFT+C and the editor creates the getter and setter methods //3

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...)

Ola Ström
  • 4,136
  • 5
  • 22
  • 41
padibro
  • 1,324
  • 10
  • 56
  • 95
  • Move the caret into the section where your property should be defined, press `CTRL + J`, choose `propgs` and fill property name and type (use `TAB` and `SHIFT + TAB` to move between those fields). – TLama Jul 10 '14 at 08:13
  • 4
    If you want to make your life easier you should start to adhere to the standard naming guidelines: the property is named `ColorText` and the field is named `FColorText`. – Uwe Raabe Jul 10 '14 at 08:15
  • @TLama with Delphi 2009 propgs does not appear in the template list - I guess it is only available with XE and newer? – mjn Jul 10 '14 at 08:19
  • 1
    You would do well to look at [MMX](http://www.modelmakertools.com/code-explorer/index.html). It includes a lot of useful functionality. To create a property you would press `Ctrl` `Alt` `P`. – Graymatter Jul 10 '14 at 08:21

3 Answers3

17

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

Andy_D
  • 2,340
  • 16
  • 21
  • Ok, but for example `GetColorText` function is empty: the IDE not auto insert `Result:=colorText;` – padibro Jul 10 '14 at 08:25
  • @bradipo, it cannot insert anything there since the IDE has no idea that your `colorText` field relates to `Colore` property. – TLama Jul 10 '14 at 08:29
  • @TLama Thats true, I work with Delphi on daily bases it still surprises me though. It could easily fill in the Setter. The getter makes sense though. Still i'm sure Embarcadero could make this work as Bradipo asked if they wanted too. – Teun Pronk Jul 10 '14 at 08:34
  • 2
    @Teun, it cannot. If you have a property with getter and setter, then you don't know anything about what's the content behind. If you define a property with a field for getter and setter, e.g. `property ColorText: string read FColorText write SetColorText;` and press `CTRL + SHIFT + C`, then the generated setter will contain a simple assignment for that field (at least in Delphi XE3). – TLama Jul 10 '14 at 08:39
  • 2
    @bradipo Getters and setters are optional in Delphi. You could just as easily write `Property Colore : String Read FColorText Write FColorText;` and the IDE will simply generate the private variable. I'll update my answer to point this out. – Andy_D Jul 10 '14 at 08:39
  • @TLama My error. I repeat: if i want define getter and setter for a variable `colo`r i want that the editor auto generate `Property color : String Read Getcolor Write Setcolor;` and the code of the getter and setter for esample `procedure ClassePippo.setcolor(const Value: String); //3 begin color:=Value; end;` – padibro Jul 10 '14 at 08:42
  • @bradipo, now I see. Well, that's not supported by the IDE. You cannot generate property with getter and setter from a field. – TLama Jul 10 '14 at 10:41
2

There is no feature in the IDE which will do what you want.

You can use Ctrl + Shift + C to generate getters and setters from a property declaration. But those getters and setters are empty stubs.

Frankly that is quite reasonable behaviour in my view. How can the IDE be expected to know how you wish to implement your getter and setter. It cannot be expected to know which field you intend to use. Your code is a good demonstration of why that is so. There is no obvious algorithmic relationship between the property name and the field name.

Another point to make is that if you want the IDE to generate the code for getter and setter in your question automatically, why do you even bother with a getter and setter? You could perfectly well write:

property colore: string read ColorText write ColorText;

If you wish to be able to use a feature as you describe you will need to find an extension to Delphi that implements the feature. The obvious candidates are CnPack and GExperts. Or if you cannot find such an extension, write one yourself.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • 1
    For what do you suggest extensions ? Besides, all what's in your post has been already said... – TLama Jul 10 '14 at 09:04
  • @TLama I suggest extensions because the IDE doesn't do what the asker wants. I honestly cannot see that my answer duplicates another answer. I'm saying that what the asker requests is not possible in the Delphi IDE. There are no features that do what he wants. The other answer, at the time of writing, does not say that at all. Can you point out where the the points that I write in my answer have already been said? – David Heffernan Jul 10 '14 at 09:17
  • I beg your pardon. I misunderstood the question. OP wants to generate a property with a getter and setter from a field, which is not supported (even by CnPack). – TLama Jul 10 '14 at 09:38
  • @TLama Not supported. That's what I said! ;-) I don't know whether or not CnPack or GExperts have anything like this, I don't use them. But I just mentioned those as obvious places to look. – David Heffernan Jul 10 '14 at 09:39
1

You can do this in three steps.

Define the property with a getter and a variable to hold the value:

property OnOff: Boolean Read GetOnOff Write fOnOff;

Press Control + Shift + C to generate the variable and getter sub:

private
fOnOff: Boolean;
function GetOnOff: Boolean;

...

And

function TMyClass.GetOnOff: Boolean;
begin
  Result := fOnOff;
end;

Now change the property to read the variable and write with a setter:

property OnOff: Boolean Read fOnOff Write SetOnOff;

Press Control + Shift + C again to generate the setter sub:

procedure TMyClass.SetOnOff(const Value: Boolean);
begin
  fOnOff := Value;
end;

Now change the property to use the getter and setter:

property OnOff: Boolean Read GetOnOff Write SetOnOff;

Yes, it's a bit convoluted but if you have a lot of properties to define at once it can help.