Reference parameter in AX 2009.

In Dynamics AX 2009 - Is there any way to pass parameter byval or byref.

The C# equivalent is :

void myFunc(ref int x)

{

}

or not by reference :

void myFunc(int x)

{

}

What is the way to send by reference?

Thanks :slight_smile:

hi,

i dont think you can pass by reference…By Default it supports only “pass by value” only…

it does not support call by reference

Hello Eitan,

In Microsoft Dynamics AX, in all X++ methods, all parameters are taken by value. In .NET, parameters can be taken by value or by reference. You can use the X++ keyword byref to call .NET methods that take parameters by reference.

1. When a parameter is passed by value, a copy of the value is given by the caller method to the called method. Assignments made by the called method to its parameter variable are not detectable by the caller.

Passing int By Value: When a primitive data type (such as an X++ int) is passed by value, a copy of the value is given to the called method.

The following code example has a section for the caller method and a section for the called method. Both are X++ methods. The called method adds 456 to its parameter variable iTest. The caller method cannot detect that the addition occurred.

Caller Method:

public void CallerMethodByValueInt() // X++

{

int iTest = 3;

;

DemoClassXpp::CalledMethodByValueInt(iTest);

if (iTest == 3)

{

info(“Good, == 3.”)

}

}

Called Method:

static public void CalledMethodByValueInt // X++

(int iTest) // by value

{

;

iTest = iTest + 456; // Caller can not detect.

}

2. When a parameter is passed by reference, the pointer (that points to the object) is given to the called method. No copy of the pointer is created. Assignments made by the called method to its parameter variable are detectable by the caller method.

The purpose of passing parameters by reference is to overcome the limitation that only one item can be returned from a method. Every parameter passed by reference is basically returned to the caller.

Passing int By Reference: When an int is passed by reference to a .NET method, the system converts the int into a new temporary object. Then the pointer to that object is given to the called method. When the called method returns, the temporary object is converted back to an int value. The value is again stored in the frame of the caller on the call stack.

Note : The operation that creates this kind of temporary object is often termed boxing.

Changes made to the int parameter variable by the called method are detectable by the caller.

The following code example has a section for the X++ caller method and a section for the C# called method. The called method adds 456 to its parameter variable iTest. The caller method can detect that the addition occurred.

Caller Method:

public void CallerMethodByReferenceInt // X++

{

int iTest = 3;

;

TheNamespace.DemoClassNet::CalledMethodByReferenceInt

(byref iTest);

if (iTest == 459)

{

info(“Good, == 459.”)

}

}

Called Method:

static public void CalledMethodByReferenceInt // C#

(ref int iTest) // by reference

{

iTest = iTest + 456; // Caller can detect.

}

You can try a workaround using the return value. I don’t now if it is a chance to you. You can define the function as:

Container myFunc(int x, int y)

{

…

Return [x, y];

}

And call it by

[a, b] = myFunc(x, y)