Implement X++ scoping and access identifiers

Completed

Scope is the area where an item is located or can be accessed. X++ variables and methods are within a defined scope. The scope can be set by the placement of declaration and the use of access identifiers.

Access identifiers are public, protected, and private.

  • Public - Methods that are declared as public can be called from anywhere that the class is accessible. In addition, a public method can be overridden by a subclass unless the method is declared as final.
  • Protected - Methods that are protected can be overridden in subclasses. Methods that are declared as protected can be called only from:
    • Methods in the class.
    • Methods in a subclass of the class that contains the protected method.
  • Private - Methods that are declared as private can be called only from methods in the class where the private method is declared. No private method can be overridden in a subclass. Private is the most conservative default for limiting code visibility.

Distinguish a generated method template from the language default. A method without an access modifier is implicitly public. An instance field is protected by default. Specify access explicitly when you need a particular boundary instead of relying on the editor template.

Internal

Internal is a keyword in X++. It has the same semantics as in C# see internal (C# Reference).

When you select a class or method as internal, it can only be accessed from within the model where it's defined.

internal class MyInternalClass
{
   internal void myInternalMethod()
   {
   }
}

Notice that you can also define internal methods on public classes.

Instance variables

Instance variables have a large scope and are also referred to as class fields in X++. These variables are declared in the class declaration and can be accessed from any method in the class and subclasses that extend the class, if they're declared public or protected.

Local variables

Local variables have a small scope. These variables are defined in a method and can only be accessed in that method.

A local variable declared inside a nested block is accessible only within that block. Declare a temporary value close to where you use it, rather than making it an instance field when no other method needs it. For example, this illustrative method-body fragment keeps the counter in the for statement and the temporary line number in its body:

for (int i = 0; i < 3; i++)
{
    int lineNumber = i + 1;
    info(int2str(lineNumber));
}

The counter i is not available after the for statement. The variable lineNumber is not available outside its block. X++ also rejects a local declaration that hides another local variable in an enclosing scope. Smaller scopes make the intended use clear and reduce accidental reuse during maintenance.

Parameters are a kind of local variable that is defined on a method.

To pass a local value from one method to another, use parameters. Methods can use multiple parameters. The parameters are treated like local variables and initialized with the value from the parameter in the method call. In the following example, the instance method has two parameters in addition to the instance variables it can access. The instance variables a and b can be accessed, and the parameters x and y act like local variables. Using parameters allows values to be assigned to those parameters when the method is called. If method1 is called on a Test instance named test by using the code test.method1("Car", 20);, the x variable is assigned the value Car, and the y variable is assigned the value 20. You can then use the values of the parameter variables to assign values to the instance variables c and d.

class Test
{
    str a = "Car";
    int b = 20;
    str c;
    int d;

    void method1(str x, int y)
    {
        c = x;
        d = y;
    }
}

The following illustrative caller fragment supplies those values:

Test test = new Test();
test.method1("Car", 20);

Keep in mind that when you pass an integer value through parameters, you don't by default change the value of the original variable. The term for this is "call by value" as opposed to "call by reference." Only the local variable is changed.

Example

In the following example, method2 has a local variable a with a value of 5. The first info() statement displays the value of a, which is 5. It calls method1 and passes the value of its local variable a. method1 now has a different local variable a, with the value of 5. method1 assigns 1 to its parameter and then increments that value by one, making a the value of 2. The info() statement then displays the value of 2. Execution returns to method2, where the second info() statement displays 5. Although both variables are named a, each belongs to a separate method scope. Changing the parameter in method1 doesn't change the local variable in method2.

class infoValue
{
    void method1(int a)
    {
        a = 1;
        a++;
        info(int2str(a));
    }

    void method2()
    {
        int a = 5;
        info(int2str(a));
        this.method1(a);
        info(int2str(a));
    }
}

The following image shows how the info displays in the user interface.

Screenshot showing how the info method displays in the user
interface.

Choose scope without assuming a performance gain

Use a parameter for a value supplied by a caller, a local variable for a value used within one method or block, and an instance field for state that the object needs across methods. In a row-by-row calculation, keep an intermediate amount local to the calculation unless another method genuinely needs that state.

A narrower scope improves clarity and limits accidental reuse. It doesn't by itself establish faster execution, and a public-to-private change isn't a measured optimization. If a scope refactor addresses an actual performance problem, compare the same behavior and workload with suitable environment-supported diagnostics, such as Trace parser. Its X++ method durations help investigate where time is spent. No benchmark or numerical improvement is claimed in this walkthrough.