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
FPC Unleashed (async/awai...
by
Fibonacci
[
Today
at 08:48:02 pm]
Network drop and firewall
by
kupferstecher
[
Today
at 07:11:07 pm]
Rounding issues (only wit...
by
Hartmut
[
Today
at 06:46:35 pm]
How to check my own certi...
by
LeP
[
Today
at 05:04:49 pm]
Review from FreePascal
by
Thaddy
[
Today
at 04:56:19 pm]
How can I get names of CO...
by
Thaddy
[
Today
at 04:55:08 pm]
Accessing VRAM memory wit...
by
Thaddy
[
Today
at 01:31:19 pm]
Fixed an RV32ec compiler ...
by
ccrause
[
Today
at 12:04:05 pm]
[SOLVED} Copying existing...
by
Davo
[
Today
at 11:59:45 am]
TDWEdit
by
Ed78z
[
Today
at 09:53:10 am]
Elite Arcade
by
Zvoni
[
Today
at 08:17:20 am]
Which quantized model wor...
by
LeP
[July 20, 2026, 10:34:07 pm]
PasFLTK - Binding for FLT...
by
Dibo
[July 20, 2026, 10:26:31 pm]
[Closed]Indy10 hangs the ...
by
marcov
[July 20, 2026, 03:52:59 pm]
Slow app start on M5 MacB...
by
pleumann
[July 20, 2026, 03:07:54 pm]
Lazarus components
by
threedslider
[July 20, 2026, 02:37:23 pm]
Many recent books on Laza...
by
threedslider
[July 20, 2026, 02:27:45 pm]
[Solved]Setting the CURSO...
by
jamie
[July 20, 2026, 11:33:52 am]
Lazarus for Windows on aa...
by
Thaddy
[July 20, 2026, 09:39:06 am]
FastCGI vs. CGI
by
egsuh
[July 20, 2026, 06:33:08 am]
I have made some progress...
by
onionmixer
[July 20, 2026, 12:32:08 am]
Lazarus Bugfix Release 4....
by
n7800
[July 19, 2026, 10:59:09 pm]
Can not load postgres cli...
by
martinm
[July 19, 2026, 10:17:31 pm]
Gtk3 widgetset - call for...
by
zeljko
[July 19, 2026, 08:21:54 pm]
an error occurred while c...
by
Seenkao
[July 19, 2026, 07:29:14 pm]
« previous
next »
Print
Pages: [
1
]
Author
Topic: [SOLVED] Able to access private member (Read 323 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: 8575
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: 89
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