Solved

Basic class with "private" variables.

Posted on 2006-06-28
5
213 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

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
Communication Between RC4 Delphi <-> PHP 3 99
Delphi selector screen 2 69
Print Graphic and Text to Epson TM-T88v 12 168
Tviruailstringtree sort multi columns on header click 1 50
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…
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…
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.
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

947 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

20 Experts available now in Live!

Get 1:1 Help Now