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
How many lines is too man...
by
creaothceann
[
Today
at 06:19:42 am]
Debian removes FPC/Lazaru...
by
valdir.marcos
[
Today
at 05:26:06 am]
could Ardour's YTK be use...
by
valdir.marcos
[
Today
at 05:13:59 am]
[ANN] PasBuild 1.5.0 rele...
by
cdbc
[
Today
at 05:10:09 am]
Commerce website written ...
by
valdir.marcos
[
Today
at 04:55:08 am]
New book on Object Pascal
by
valdir.marcos
[
Today
at 04:30:08 am]
How to execute a procedur...
by
Aruna
[
Today
at 03:34:41 am]
BGRAVirtualScreen - weird...
by
backprop
[
Today
at 03:33:51 am]
The "dockedformeditor" pa...
by
Gustavo 'Gus' Carreno
[
Today
at 02:44:31 am]
unit ProjectDescriptorTyp...
by
n7800
[
Today
at 02:01:39 am]
TDirectoryEdit with OnAft...
by
dsiders
[
Today
at 12:20:23 am]
Canvas.StretchDraw, does ...
by
jamie
[March 06, 2026, 11:27:18 pm]
Front-end framework
by
kveroneau
[March 06, 2026, 08:00:37 pm]
REST Server/Client, how t...
by
kveroneau
[March 06, 2026, 07:35:25 pm]
Variable initialization
by
LeP
[March 06, 2026, 06:58:53 pm]
[SOLVED] Problem to resto...
by
Hartmut
[March 06, 2026, 06:42:28 pm]
Text Fill (Memo)
by
Sc0li0sis
[March 06, 2026, 06:08:54 pm]
BGRA Controls license inf...
by
Zoran
[March 06, 2026, 05:48:27 pm]
TCalendar Assign a Date
by
eldonfsr
[March 06, 2026, 05:04:10 pm]
Update a table with an Au...
by
wp
[March 06, 2026, 04:16:29 pm]
[Feature Request]Add Larg...
by
TYDQ
[March 06, 2026, 03:03:31 pm]
Receipt printers & lazare...
by
Petrus Vorster
[March 06, 2026, 02:36:22 pm]
STOMP Clients Release 202...
by
mjustin
[March 06, 2026, 01:56:25 pm]
FreePascal/Lazarus docume...
by
MarkMLl
[March 06, 2026, 10:03:46 am]
PasLLM - LLM Inference En...
by
domasz
[March 06, 2026, 08:53:15 am]
« previous
next »
Print
Pages: [
1
]
Author
Topic: [SOLVED] Able to access private member (Read 240 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: 8548
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: 85
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