Search code examples
delphipropertiesideautomatic-properties

How to create an auto-property faster in Delphi IDE?


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?


Solution

  • 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: