Microsoft Dynamics AX 2012 R3 Development-2 Locus IT Services

Microsoft Dynamics AX 2012 R3 Development-2

Locus IT ServicesMicrosoft Dynamics AX 2012 R3 Development-2

Lesson 6 – Scoping and Parameters in X++

Scoping and Parameters in X++:

The scope rules in X++ state that all methods have their own scope. To use data from one scope in a different scope, you have to transfer the data from one scope into the other scope by using parameters.

A method can take one or more parameters. Within the scope of the method, these parameters are treated like local variables and are initialized with the value from the parameter in the method-call. The following table shows an example.

Here, a class called Parameter is declared. The class has a method (methodQ), which takes two parameters: an integer and a real. Four variables are visible within the scope of methodQ: aval, bval, a, and b. The a and b variables are in the scope because they are defined in the class declaration for the class.

Assume that Parameter Object is an instance of the Parameter class. If methodQ is invoked on Parameter Object, ParameterObject.methodQ(1,2.0); aval is set to the value 1 within the method and the variable bval is set to 2.0 (it is a real) within the method. The two variables a and b in the ParameterObject are set to 1 and 2.0, respectively. You can pass the value of any expression as a parameter.

Parameters Are Passed by Value

All parameters are passed by value. You cannot change the value of the original variable—you can change only the local variable in the method, which is a copy of the original. The following table shows an example.

Class declaration Method methodA Method methodB
class ByValue void methodA(int i) void methodB()
{ { {
}     i = i + 1;     int i = 3;
    print i;     print i;
}     this.methodA(i);
    print i;
Locus IT Project Management Office
What’s it?