Working with internal access modifiers and X++

MS recently anncounced that they are changing the default class access modified to internal to follow the C# practise for X++. Read more here

This is not as scary change, it just means that the public access will be limited, narrowing down the possible calls you can make between models if you choose to use the default access modifier. 

Internal classes and methods work like public but are restricted to usage by other objects inside the same model. A model can be configured to expose all internal objects and methods to another model. 

For example, below the internal class can only be used inside the model in which it's declared or by a model to which the model containing class have been exposed. Same applies to the internal method. 

internal class HelloWorldCanOnlyBeUsedInsideModel {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

public class HelloWorld {

    internal static void mainCanOnlyBeCalledInsideModel(String[] args) {
        System.out.println("Hello World!");
    }
}

To expose internal objects and methods to another model use a text editor and edit the model descriptor file, add the models that should have access to the internals by adding them to the <InternalsVisibleTo> tag. 

For see for example example K:\AosService\PackagesLocalDirectory\ApplicationSuite\Descriptor\Foundation.xml . 


Common issue with internals occur when they are used in forms and table objects which are public objects. The compiler will throw errors if variable of an internal type is added as public as it a more open access modifier than internal. The simple way to fix the issue is to change the variable to private.

Good to know also is that MS doesn't clean up their test models in their model descriptor files. Creating a model with the same name as their test models gives you access to be able to call internal methods which can be good to know for testing some code before requesting an extension point. I written about this before in a post here

Comments

Popular posts from this blog

How to disable auto enabled flight key using KillSwitch

Technical intro to Feature management in D365FO

Continuous integration and deployment Power platform FinOps tweaking