How To: PROPERTY INIT

This forum is meant for questions and discussions about the X# language and tools
Post Reply
leon-ts
Posts: 435
Joined: Fri Feb 03, 2017 1:43 pm

How To: PROPERTY INIT

Post by leon-ts »

Hi,

I read in the X# documentation in the "PROPERTY Statement" section about the possibility to specify an INIT accessor.

Quote from X# documentation:
INIT accessor declare that a property can only be changed in the constructor of a class. A property cannot have both a SET and an INIT accessor.
Wrote the code:

Code: Select all

CLASS MyClass
	PROPERTY MyProperty AS INT AUTO GET INIT
END CLASS
The compiler throws an error:
XS0518 Predefined type 'System.Runtime.CompilerServices.IsExternalInit' is not defined or imported
What is the correct use of the INIT keyword in a PROPERTY declaration?

Best regards,
Leonid
Best regards,
Leonid
User avatar
Chris
Posts: 4906
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

How To: PROPERTY INIT

Post by Chris »

Hi Leonid,

This looks like either a compiler bug, or that the compiler requires a newer a .Net framework version. Which one do you have installed?
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
leon-ts
Posts: 435
Joined: Fri Feb 03, 2017 1:43 pm

How To: PROPERTY INIT

Post by leon-ts »

Hi Chris,
I have Windows 10 installed which has .NET Framework 4.8 included.
In X# applications I develop, I specify TargetFramework = 4.7.2

Best regards,
Leonid
Best regards,
Leonid
User avatar
robert
Posts: 4520
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

How To: PROPERTY INIT

Post by robert »

Leonid,
The INIT keyword requires either .Net 5 or you will have to add the following class to your app:

Code: Select all

// This static class is needed when not compiling against .Net 5
BEGIN NAMESPACE System.Runtime.CompilerServices
    INTERNAL STATIC CLASS IsExternalInit 
    END CLASS
END NAMESPACE
See also
https://github.com/X-Sharp/XSharpDev/bl ... g/R766.prg

Robert
XSharp Development Team
The Netherlands
robert@xsharp.eu
leon-ts
Posts: 435
Joined: Fri Feb 03, 2017 1:43 pm

How To: PROPERTY INIT

Post by leon-ts »

Robert,
Thanks a lot! It works!

Best regards,
Leonid
Best regards,
Leonid
Post Reply