14

I need to create and manage many simple published properties. I call them auto-properties if they look like that:

private
  FTitle: string;
published
  property Title: string read FTitle write FTitle;

Usually I create them next way:

  1. Adding property name and type:

    property Title: string

  2. Selecting and copying property name Title to clipboard.

  3. Appending the line with read F Ctrl+V write F Ctrl+V ;

  4. Pressing Ctrl+Shift+C and this will generate the private field

Is there any way to exclude steps 2 and 3 to add properties faster? Maybe it is possible to create some macro for this?

Ken White
  • 123,280
  • 14
  • 225
  • 444
Andrew
  • 3,696
  • 3
  • 40
  • 71
  • 6
    Er... You can usually skip steps 2 and 3 by skipping steps 2 and 3. It creates a procedure to set the field, but that procedure does nothing more than set the field. Is that undesirable for you for some reason? –  May 30 '12 at 12:18
  • wow! Never tried this. But without procedures it would be better. Don't want to flood the class definition. – Andrew May 30 '12 at 12:24
  • 3
    Just write `property Something: Boolean;` and press `CTRL + SHIFT + C` and CodeCompletion will create a property field skeleton. And anyway those *procedures* are called setters. – TLama May 30 '12 at 12:28
  • @TLama Any secret IDE option to disable the creation of setters by Ctrl+Shift+C? – Andrew May 30 '12 at 12:29
  • I suppose if you really want, you could create all properties with Ctrl-Shift-C, and then replace `write Set` with `write F` and remove the `Set` procedures in one go. –  May 30 '12 at 12:51
  • 3
    Using LiveTemplates (introduced in Delphi 2006 iirc) you can type propf and hit Ctrl+J. – Stefan Glienke May 30 '12 at 13:04

3 Answers3

20

Since Delphi 2006 you can use LiveTemplates.

In Delphi XE for example:

  • type propf and hit Ctrl + J keystroke
  • write the property name and hit TAB key
  • write the property type name, hit TAB or ENTER key and you are done

You can find more information on how to write your own Live Templates here:

TLama
  • 75,147
  • 17
  • 214
  • 392
Stefan Glienke
  • 20,860
  • 2
  • 48
  • 102
5

I use macros for that purpose.

For example I have model with fields

private
  FTitle: string;
  FName: string
  FAge: Integer

then I copy-paste the fields into published section and create macro

  1. Goto first field and hit Home
  2. Hit Ctrl + Shift + R to start recording macro
  3. Use Crtl + ->, Crtl + <- and End keys for navigation and convert first field to property like property Title: string Read FTitle Write FTitle;
  4. After that hit Home and go to next row
  5. Finish macro by hitting Ctrl + Shift + R
  6. For all other fields, just press Crtl + Shift + P

At first it seems difficult, but the skills will pay off.

Raido
  • 571
  • 5
  • 11
1

In XE7 type prom and hit enter. It seems faster.