Solved

Making a procedure/function private to a unit

Posted on 2002-03-12
3
163 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

762 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now