xsharp.eu • ReadOnly Property
Page 1 of 1

ReadOnly Property

Posted: Tue Sep 04, 2018 8:23 am
by orangesocks
Hi,
is there the possibility to declare an auto property of a class as readonly ?
IF yes, can you share a little sample because I tried various combinations and all failed :-(

Regards Giuseppe

ReadOnly Property

Posted: Tue Sep 04, 2018 8:37 am
by robert
Giuseppe,
I don't think you can create a readonly auto property.
But try this:

Code: Select all

CLASS Example
    // Not strictly readonly but can only be changed inside class
    PROPERTY NAME AS STRING AUTO GET PRIVATE SET
CONSTRUCTOR(cName AS STRING)                    
	SELF:Name := cName
END CLASS
or this:

Code: Select all

CLASS Example2
    // Really readonly. Can only be assigned in the constructor
    PROPERTY NAME AS STRING GET _Name
    PRIVATE INITONLY _Name AS STRING
CONSTRUCTOR(cName AS STRING)                    
	SELF:_Name := cName
END CLASS

ReadOnly Property

Posted: Tue Sep 04, 2018 8:46 am
by orangesocks
Robert,
thanks for the explanation. Both work.
Regards Giuseppe