Solved

Basic class with "private" variables.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

740 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