Lazarus
Home
Help
TinyPortal
Search
Login
Register
Lazarus
»
Forum
»
Free Pascal
»
General
(Moderators:
FPK
,
Tomas Hajny
) »
[SOLVED] Able to access private member
Free Pascal
Website
Downloads
Wiki
Documentation
Bugtracker
Mailing List
Lazarus
Website
Downloads (Laz+FPC)
Packages (OPM)
FAQ
Wiki
Documentation (RTL/FCL/LCL)
Bugtracker
CCR Bugs
GIT
Mailing List
Other languages
Foundation
Website
Useful Wiki Links
Project Roadmap
Getting the Source
Screenshots
How to use the forum
Forum Rules
About donations (wiki)
Bookstore
Computer Math and Games in Pascal
(preview)
Lazarus Handbook
Search
Advanced search
Recent
Error installing EyeCandy...
by
madref
[
Today
at 12:46:05 am]
Lazarus undead repository...
by
CM630
[August 11, 2026, 11:40:09 pm]
LRS Explorer
by
CM630
[August 11, 2026, 11:33:24 pm]
Identify network devices
by
Warfley
[August 11, 2026, 10:55:13 pm]
The best name for procedu...
by
J-G
[August 11, 2026, 07:49:19 pm]
var vs out
by
Paolo
[August 11, 2026, 06:37:35 pm]
[UNSOLVED] Splitting an i...
by
madref
[August 11, 2026, 05:40:09 pm]
Lazarus Bugfix Release 4....
by
geraldholdsworth
[August 11, 2026, 04:55:42 pm]
[Solved[ How to define th...
by
Mike.Cornflake
[August 11, 2026, 03:42:11 pm]
Dataset EnableControls/Di...
by
Mike.Cornflake
[August 11, 2026, 03:37:42 pm]
'ReadStr' ?
by
J-G
[August 11, 2026, 03:07:16 pm]
Debugging in a real termi...
by
Martin_fr
[August 11, 2026, 02:59:09 pm]
how to bring in view the ...
by
Aruna
[August 11, 2026, 02:33:55 pm]
Is it possible to communi...
by
dbannon
[August 11, 2026, 02:15:44 pm]
TFPHTTPServer DDoS protec...
by
LemonParty
[August 11, 2026, 12:54:23 pm]
enable easy marker moveme...
by
Paolo
[August 11, 2026, 12:10:00 pm]
Unleashed Pascal (async/a...
by
creaothceann
[August 11, 2026, 12:09:09 pm]
MOVED: soucis scrollbars ...
by
Martin_fr
[August 11, 2026, 11:09:38 am]
soucis scrollbars V4.8 ?
by
Martin_fr
[August 11, 2026, 11:08:13 am]
Star Wars Galaxian
by
PierceNg
[August 11, 2026, 10:37:08 am]
[closed] Is case for stri...
by
egsuh
[August 11, 2026, 10:27:17 am]
Automatic generation of *...
by
dbannon
[August 11, 2026, 09:00:50 am]
Gtk3 becomes default widg...
by
dbannon
[August 11, 2026, 08:44:10 am]
TurboBird for FireBird 5
by
laguna
[August 11, 2026, 07:45:04 am]
Bugs caused by the weathe...
by
dsiders
[August 11, 2026, 04:40:59 am]
« previous
next »
Print
Pages: [
1
]
Author
Topic: [SOLVED] Able to access private member (Read 337 times)
vinntec
New Member
Posts: 11
[SOLVED] Able to access private member
«
on:
January 12, 2026, 09:09:29 am »
I must have done something stupid, but I can't see what. I am following a book "The Modern Day Pascal Developer" as I get back into using Pascal (used UCSD Pascal and Turbo Pascal in the past). One of the examples is to demonstrate encapsulation which demonstrates that you cannot access a private class member from outside the class. However, if I uncomment the line to access the private member FValue from main and add a second list, the private member is being changed. What have I done wrong?
Code: Pascal
[Select]
[+]
[-]
program
EncapsulationDemo
;
{$mode objfpc}{$H+}
type
TCounter
=
class
private
FValue
:
Integer
;
{ hidden }
public
procedure
Increment
;
procedure
Reset
;
procedure
Show
;
end
;
procedure
TCounter
.
Increment
;
begin
Inc
(
FValue
)
;
end
;
procedure
TCounter
.
Reset
;
begin
FValue
:
=
0
;
end
;
procedure
TCounter
.
Show
;
begin
Writeln
(
'Counter = '
,
FValue
)
;
end
;
var
C
:
TCounter
;
begin
C
:
=
TCounter
.
Create
;
C
.
Reset
;
C
.
Increment
;
C
.
Increment
;
C
.
Show
;
{ should print 2 }
C
.
FValue
:
=
100
;
// not allowed: FValue is private
C
.
Show
;
{ should print 2 }
C
.
Free
;
Writeln
(
'Press Enter to exit...'
)
;
Readln
;
end
.
Code: Pascal
[Select]
[+]
[-]
Counter
=
2
Counter
=
100
Press Enter
to
exit
...
«
Last Edit: January 12, 2026, 09:30:27 am by vinntec
»
Logged
MarkMLl
Hero Member
Posts: 8578
Re: Able to access private member
«
Reply #1 on:
January 12, 2026, 09:17:16 am »
-----8<-----
Private
All fields and methods that are in a private block, can only be accessed in the module (i. e. unit) that contains the class definition. They can be accessed from inside the classes’ methods or from outside them (e. g. from other classes’ methods)
Strict Private
All fields and methods that are in a strict private block, can only be accessed from methods of the class itself. Other classes or descendent classes (even in the same unit) cannot access strict private members.
----->8-----
https://www.freepascal.org/docs-html/current/ref/refse35.html#x70-940006.1
For that reason I normally use strict private throughout.
MarkMLl
Logged
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories:
https://github.com/MarkMLl?tab=repositories
Nimbus
Full Member
Posts: 104
Re: Able to access private member
«
Reply #2 on:
January 12, 2026, 09:18:22 am »
See difference between
private
and
strict private
modifiers:
https://castle-engine.io/modern_pascal#_private_and_strict_private
https://www.freepascal.org/docs-html/ref/refse35.html
Logged
vinntec
New Member
Posts: 11
Re: Able to access private member
«
Reply #3 on:
January 12, 2026, 09:27:53 am »
Thank you guys - that explains it. If I move the class into a unit by itself then private works as expected. But now I know that private means I can access from the same unit (single file in this case) so need to use strict private in those cases.
Logged
Print
Pages: [
1
]
« previous
next »
Lazarus
»
Forum
»
Free Pascal
»
General
(Moderators:
FPK
,
Tomas Hajny
) »
[SOLVED] Able to access private member
TinyPortal
© 2005-2018