Useful Delphi code templates

2020-05-27 16:18发布

问题:

I've been programming in Delphi for a little over two years now, and I've only got maybe 5 custom templates, I feel as though I should have more.

If anyone has any particularly useful ones it would be great to have a nice repository of them here on stackoverflow.

I don't care whether you use the Delphi 2009 syntax or Delphi 7, but if the code you'd be generating only works on a particular version, please mention that.

回答1:

There are a handful of useful ones here, including a few based on CodeRush. These are the live templates, so they work in Delphi 2006 and up.

http://delphi.wikia.com/wiki/Delphi_Live_Templates



回答2:

Very handy output debug string plus formatting


Name:
ods
Description:
Output Debug String with Format Code:

OutputDebugString(PAnsiChar(format('|',[])));

also, if I want to leave it in the code:

Name:
dods
Description:
Output Debug String with ifdef debug
Code:

{$IFDEF DEBUG}   
   OutputDebugString(PAnsiChar(format('|',[])));   
{$ENDIF}


回答3:

This one is pretty handy because it always shows up first when I hit ctrl-j (so my shortcut is ctrl-j enter)


Name:
//*
Description:
Comment Line
Code:

//******************************************************************************  

I use it to start documentation around methods. It sure beats hitting '*' 78 times.



回答4:

If you do a lot of work on datasets these are handy.

Name: fbn

Description: DataSet FieldByName

|DataSet|.FieldByName( '|Field|' )|end|

Name: pbn

Description: DataSet ParamByName

|DataSet|.ParamByName( 'P_|Param|' )|end|


回答5:

I did a couple that saved me lots of time. A few of those took the monotone out of property declarations. Then I did some to create type-safe enumerators, object lists and bucket lists. I probably should do one for collections too, but haven't yet.

These are all available on my blog.