I'm a Java developer and I always use the getter-setter methods. How can I use this concept in Delphi?
//1
//2
//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...)
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
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With