Re: Procedures order in Delphi unit

Posted by webmaster Guido

In Reply to: Procedures - Is there any order they should be placed in coding ? posted by Lionel E Joyner:

: When writing up an application is there any order that procedures should follow ? For example: Can you put your print procedure at the beginning of your code or should it be the last procedure ?
: Does it matter where in you place a procedure in Delphi?

If a routine is also declared in the interface section of a unit, this acts as a "forward" declaration. In that case, its location (order) in the "implementation" section doesn't matter. That's why Delphi programmers often write routines as "methods" of a form, because then they are always declared in the interface section (under private or under public).

An example:

...
private
   // First, all of the private variables, e.g.:
  N1, N2, N3: integer;
  // and so on...
  // Next, the private methods, in any order:
  procedure Print123; 
  ...

implementation
...
procedure TForm1.Print123;
begin
    ...
end;

But if the procedure Print123 is declared as a "normal" routine (not as a method) and it isn't mentioned in the interface, then it must be located BEFORE any code that uses this procedure.

So, this example works:

implementation
...
procedure Print123;
begin
  ...
end;

procedure TForm1.Button1OnClick(...);
begin
Print123; // works, Print123 declared before Button1OnClick end;

But the following example does not work:

implementation
  ...
procedure TForm1.Button1OnClick(...);
begin
  PrintSomething; // error: Print123 must be declared before Button1OnClick
end;
  
procedure Print123;
begin
  ...
end;

Related Articles and Replies


DelphiLand Discussion Forum