ReadOnly Property

This forum is meant for questions and discussions about the X# language and tools
Post Reply
User avatar
orangesocks
Posts: 40
Joined: Thu Nov 12, 2015 2:41 pm

ReadOnly Property

Post 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
User avatar
robert
Posts: 4519
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

ReadOnly Property

Post 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
XSharp Development Team
The Netherlands
robert@xsharp.eu
User avatar
orangesocks
Posts: 40
Joined: Thu Nov 12, 2015 2:41 pm

ReadOnly Property

Post by orangesocks »

Robert,
thanks for the explanation. Both work.
Regards Giuseppe
Post Reply