Solved

Basic class with "private" variables.

Posted on 2006-06-28
5
216 Views
Last Modified: 2010-04-05
Hi.

I've created a class for use in an application and I'm having problems understanding the point of the word "private", as it seems to make no difference.

For example, here's a small class that shows my problem...

type
  TTestClass = class
    private
      _Name : String;
      procedure SetName(Value: String);
    public
      property Name : String read _Name write SetName;
    end;

procedure TTestClass.SetName(Value: String);
begin
  _Name := Value;
end;


if I create an instance of that class, then I would not expect to be able to do the following...

  oTestClass := TTestClass.Create;
  oTestClass._Name := 'Bob';

...but I can.

Can someone PLEASE tell me what the point of the "private" keyword is, AND how do I make a truly private variable that is for internal use within the class only?

Thanks in advance,

John.

PS. It's lots of points cos I need a quick answer.
0
Comment
Question by:Jaymol
  • 3
  • 2
5 Comments
 
LVL 10

Expert Comment

by:atul_parmar
ID: 16999729
the scope of private is upto unit level not class level.
0
 
LVL 10

Accepted Solution

by:
atul_parmar earned 250 total points
ID: 16999738
Try to do the same in another unit.

oTestClass := TTestClass.Create;
oTestClass._Name := 'Bob'; // >>> here you will get an error
0
 
LVL 6

Author Comment

by:Jaymol
ID: 16999872
Okay, I see that now.

Is there simply no way to make private declarations actually private within the same unit then?

John.
0
 
LVL 10

Expert Comment

by:atul_parmar
ID: 16999886
NO Not in Delphi 1-7. However, in Delphi 2005/2006 there is strict private what could make it. e.g.
...
strict private
  _Name : String;
...
0
 
LVL 6

Author Comment

by:Jaymol
ID: 16999912
Ah well.

Guess I'll just have to bear that in mind for the future.  I assumed that the variables weren't actually private, but will just stick things in separate units from now on.

Thanks for the help.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Error E2158 compiling with Delphi XE10 Seattle 2 112
Delphi Yen format 3 45
how to change, disabled button color FMX ? 1 40
Twebbrowser add css to the header 3 27
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…
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

856 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