Link to home
Start Free TrialLog in
Avatar of Richard2000
Richard2000

asked on

Making a procedure/function private to a unit

Hi,

I have two procedures in a unit, one I want to be available outside the unit (public) and the other should only be available to the unit itself (private).  The code below demonstates what I am trying to do, but it won't compile.  PrivateProc is called by PublicProc and should only be private.  If I move the "procedure PrivateProc;" declaration to the interface section, it will compile, however PrivateProc no longer is private anymore.  What do I need to do?

Thanks in Advance,

Richard

Here is the code...

unit Unit2;

interface

procedure PublicProc;

implementation

uses
  Dialogs;

procedure PrivateProc;

procedure PublicProc;
begin
  ShowMessage('PublicProc');
  PrivateProc;
end;

procedure PrivateProc;
begin
  ShowMessage('PrivateProc');
end;

end.
Avatar of DrDelphi
DrDelphi

implement the "private" procedure before the published one. For example:

interface
uses dialogs,forms,sysutils;

Procedure Foo; /// available to all

implementation

Procedure Bar; // internal to this unit
begin
  Showmessage(TimeTostr(now));
end;

Procedure Foo; /// implementation of prototyped procedure
begin
  Bar;
end;





Good luck!!
You cannot declare private procedures at the start of the implementation section, so you need to write your private procedures before you call them. DrDelphi already wrote an example.

---

unit Unit2;

interface

procedure PublicProc;

implementation

uses
 Dialogs;

procedure PrivateProc; // This line causes the compilation error, so get rid of it

procedure PublicProc;
begin
 ShowMessage('PublicProc');
 PrivateProc;
end;

procedure PrivateProc; // Write this before you call it from the PublicProc
begin
 ShowMessage('PrivateProc');
end;

end.
---
ASKER CERTIFIED SOLUTION
Avatar of TOndrej
TOndrej

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial