Solved

Basic class with "private" variables.

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

744 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