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
; after then
by
n7800
[
Today
at 09:51:10 pm]
[ANN] fpGUI Toolkit v2.0....
by
cdbc
[
Today
at 09:40:04 pm]
Reverting to Old AnchorDo...
by
dsiders
[
Today
at 09:32:39 pm]
Form designer on MAC: Des...
by
tk
[
Today
at 09:29:44 pm]
Select rectangle of the i...
by
Phoenix
[
Today
at 09:28:35 pm]
Fast Canvas Library V1.05...
by
backprop
[
Today
at 08:15:33 pm]
Strange Error in My Code ...
by
bytebites
[
Today
at 07:55:44 pm]
TRichMemo EM_FORMATRANGE ...
by
rvk
[
Today
at 07:54:30 pm]
[SOLVED] Sort DBGrid
by
Hansvb
[
Today
at 07:49:56 pm]
fpGUI Toolkit v2.0.1 has ...
by
Graeme
[
Today
at 07:43:51 pm]
Fixing Lazarus 4.4 IDE la...
by
mdalacu
[
Today
at 06:59:50 pm]
[SOLVED] Lazarus recompil...
by
Hartmut
[
Today
at 06:00:38 pm]
Lazarus for Windows on aa...
by
AlexTP
[
Today
at 05:57:57 pm]
Status of FPC 3.4.0 or FP...
by
creaothceann
[
Today
at 05:42:35 pm]
Debian removes FPC/Lazaru...
by
sfeinst
[
Today
at 04:14:28 pm]
Defining TDBDropDownCombo...
by
1HuntnMan
[
Today
at 04:12:04 pm]
Rolling releases Lazarus[...
by
ALLIGATOR
[
Today
at 03:28:46 pm]
How to determine the unkn...
by
CM630
[
Today
at 03:10:40 pm]
[SOLVED] TaChart on MAC: ...
by
tk
[
Today
at 03:02:08 pm]
DCPcrypt v2.0.6 — Cryptog...
by
cdbc
[
Today
at 02:05:57 pm]
Free Pascal for a small e...
by
avra
[
Today
at 02:03:26 pm]
Fpcupdeluxe
by
tk
[
Today
at 12:08:34 pm]
ThorVG - test (lightweigh...
by
Boleeman
[
Today
at 11:28:52 am]
External SIGSEV (Database...
by
Zvoni
[
Today
at 08:26:34 am]
fpGUI vs MSEide + MSEgui?
by
Graeme
[February 10, 2026, 10:23:25 pm]
« previous
next »
Print
Pages: [
1
]
Author
Topic: [SOLVED] Able to access private member (Read 220 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: 8533
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
Jr. Member
Posts: 84
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