The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 2,231 other followers

Small Delphi tricks: initialise out parameters in the caller when they are stored in local variables; a Run for TProc

Posted by jpluimers on 2020/12/29

Two small Delphi tricks:

On out parameters

When you have out parameters, and the caller passes local variables for them, remember that:

  1. the compiler then will not issue an “unitialised variable” warning any more because it expects the callee to fill that parameter
  2. if the local variable is non-managed, it will have a random value depending on the call stack state

A RunProc method that executes a TProc

Every so often you want to refactor a method to use two or more different algorithm implementations.

A good start is to have some conditional, to choose the algorithm, then have anonymous methods implement each algorithm.

This quickly gives you a feel of where the local vars of the original method need to go:

  • local to an implementation
  • local to the original method
  • parameters to the algorithm

A parameterless runner can be a good start to call these methods:

uses
   System.SysUtils;
/...
procedure RunProc(const AProc: TProc);
begin
  AProc();
end;

You can extend this to TProc<T> and further generic forms, by adding parameters. In that case however, the need to be inside a record (because global methods cannot be generic).

–jeroen

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

 
%d bloggers like this: