Solved

Making a procedure/function private to a unit

Posted on 2002-03-12
3
165 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
TVirtualStringTree  search using TEdit 7 121
oracle global variables 4 69
Working with hours 3 58
How to Get Images From Server to Client using App Tethering 1 12
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

809 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