Solved

Making a procedure/function private to a unit

Posted on 2002-03-12
3
166 Views
Last Modified: 2010-04-05
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.
0
Comment
Question by:Richard2000
3 Comments
 
LVL 6

Expert Comment

by:DrDelphi
ID: 6858325
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!!
0
 

Expert Comment

by:Kelmi
ID: 6858541
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.
---
0
 
LVL 8

Accepted Solution

by:
TOndrej earned 50 total points
ID: 6858636
You can also use the "forward" keyword:

implementation

uses
 Dialogs;

procedure PrivateProc; forward;

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

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

HTH
TOndrej
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

827 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question