Arthur Arthur - 2 months ago 15
Pascal Question

How to use Unit files in Delphi

I'm just trying to get the hang of separate units to make my code more encapsulated.
I'm trying to get the public/private declarations of my methods sorted out, so I can call them from other units that use

testunit
. In this example I want to make
hellofromotherunit
public, but
stickletters
private.

unit testunit;

interface

uses
Windows, Messages, Dialogs;

implementation

function stickletters(a,b:string):string;
begin
result:=a+b;
end;

procedure hellofromotherunit();
begin
showmessage(stickletters('h','i'));
end;

end.


I could not seem to copy the private/public structure from other units as in:

Type
private
function stickletters(a,b:inter):integer;
public
procedure hellofromotherunit();
end

Answer

The Unit structure looks a bit like the public/private sections from objects, you could say it is their forerunner. But the syntax is different.

You only have to declare the method header in the interface section, as in:

interface
  procedure hellofromotherunit();

implementation
  procedure hellofromotherunit(); begin .. end;

Only one of each section allowed.

Comments