Solved

Making a procedure/function private to a unit

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi OLE Error 8 95
Newbie Thread Programming 1 139
Mobile Keyboard covers the display of the TMemo 3 78
Magic Software info 18 125
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

896 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

16 Experts available now in Live!

Get 1:1 Help Now