[fpc-pascal] property or public

Joao Morais post at joaomorais.com.br
Mon Jan 21 15:00:23 CET 2008


Damien Gerard wrote:
> 
> On Jan 21, 2008, at 2:52 PM, Joao Morais wrote:
> 
>> Damien Gerard wrote:
>>> I have (it would seem) a stupid question :)
>>> We have TStringList vars. User can do what he want with it.
>>> Which one is the stupid or the better way to do it ?
>>> TMyClass = class(TObject)
>>> public
>>>   <snip>
>>>   List1: TStringList;
>>>   List2: TStringList;
>>> end;
>>> or
>>> TMyClass = class
>>> private
>>>   FList1: TStringList;
>>>   FList2: TStringList;
>>> public
>>>   property List1: TStringList read FList1;
>>>   property List2: TStringList read FList2;
>>> end;
>>
>> The later, *much* better. You should never use class members outside 
>> the private area.
> 
> Thanks ! What is the reason ? I am happy I was right but I need some 
> reason :)

Encapsulation, on behalf of the integrity of the instance.

--
Joao Morais



More information about the fpc-pascal mailing list