Useful Delphi code templates
Asked Answered
F

5

7

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.

Fiora answered 19/5, 2009 at 21:6 Comment(0)
L
11

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

Leilani answered 19/5, 2009 at 21:6 Comment(0)
F
8

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}
Fiora answered 19/5, 2009 at 21:6 Comment(4)
We have a little procedure ODS(const s: string) instead. :)Hexapartite
wow i have all most the same shortcut, but with out the format, I think im going to add that now :) thanksPuling
Little reminder from the unicode conversion front - change to OutputDebugStringW or OutputDebugStringA accordinglyFiora
Use PChar instead of PAnsiChar. That way, it'll work fine both in Unicode and non-Unicode versions of Delphi.Inellineloquent
F
5

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.

Fiora answered 19/5, 2009 at 21:6 Comment(3)
I use a similar one to this one, but its named hr and is a line of dashes rather than asterisks.Metametabel
I can't think of where I'd put such a comment that would actually be useful. I.e. in the sense of explaining what the code does.Periodicity
That's the first part of a useful comment, (i.e. //********** //* This function does something awesome //*************) My company requires at a minimum //************** between procedures, which can get tedious when you're a guy who likes to use properties with get/set methods.Fiora
T
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|
Tetrameter answered 19/5, 2009 at 21:6 Comment(1)
I use a program to auto-generate those codes. Code-complete-40-times is still too many.Scout
R
3

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.

Rhizo answered 19/5, 2009 at 21:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.