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
ThorVG - test (lightweigh...
by
ALLIGATOR
[
Today
at 06:15:16 am]
Dumping preprocessed work...
by
440bx
[
Today
at 01:59:14 am]
[ANN] PasBuild 1.2.0 Rele...
by
Graeme
[
Today
at 12:20:34 am]
Star Trek playing cards a...
by
TBMan
[February 08, 2026, 10:02:47 pm]
it2play - IT/S3M module r...
by
Lulu
[February 08, 2026, 09:50:23 pm]
Fast Canvas Library V1.05...
by
backprop
[February 08, 2026, 09:24:18 pm]
Lazaris IDE v4.4 - extrem...
by
CM630
[February 08, 2026, 08:10:36 pm]
How to determine the unkn...
by
CM630
[February 08, 2026, 08:06:39 pm]
Pre-Specialize Generic Fu...
by
MMarie
[February 08, 2026, 08:04:53 pm]
Change colors for Code Ex...
by
TBMan
[February 08, 2026, 07:37:40 pm]
Can we wallgarden this, p...
by
Mobius1
[February 08, 2026, 07:21:06 pm]
Testers needed - Skip met...
by
Martin_fr
[February 08, 2026, 05:10:28 pm]
FPC and Lazarus coding st...
by
Curt Carpenter
[February 08, 2026, 04:10:03 pm]
Status of FPC 3.4.0 or FP...
by
Martin_fr
[February 08, 2026, 11:44:46 am]
Here's how to show colour...
by
Pallzi
[February 08, 2026, 11:19:27 am]
Transparent Form: Done di...
by
LV
[February 08, 2026, 08:38:31 am]
Lazarus for Windows on aa...
by
msintle
[February 08, 2026, 02:29:50 am]
Debian removes FPC/Lazaru...
by
dbannon
[February 08, 2026, 12:57:16 am]
Rolling releases Lazarus[...
by
dbannon
[February 08, 2026, 12:44:27 am]
GridPrinter requirements ...
by
sfeinst
[February 08, 2026, 12:39:07 am]
Lazarus programs look ugl...
by
the_magik_mushroom
[February 07, 2026, 11:54:12 pm]
LCL Web Native with D2Bri...
by
xinyiman
[February 07, 2026, 10:43:41 pm]
Recommendations for wasm3...
by
PascalDragon
[February 07, 2026, 10:38:15 pm]
Migrating SK1 Project to ...
by
jamie
[February 07, 2026, 09:13:45 pm]
[SOLVED] The main screen ...
by
Hansvb
[February 07, 2026, 06:50:52 pm]
« previous
next »
Print
Pages: [
1
]
Author
Topic: [SOLVED] Able to access private member (Read 212 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: 8527
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