xsharp.eu • How To: PROPERTY INIT
Page 1 of 1

How To: PROPERTY INIT

Posted: Sat Aug 07, 2021 6:27 pm
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

How To: PROPERTY INIT

Posted: Sat Aug 07, 2021 6:55 pm
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?

How To: PROPERTY INIT

Posted: Sat Aug 07, 2021 7:15 pm
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

How To: PROPERTY INIT

Posted: Sat Aug 07, 2021 7:26 pm
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

How To: PROPERTY INIT

Posted: Sat Aug 07, 2021 7:35 pm
by leon-ts
Robert,
Thanks a lot! It works!

Best regards,
Leonid