As in other programming languages, in AL we can pass a variable by value or by reference. In this short post, you will learn what is the difference between them. Also, you will learn that some data types only support passing by reference in Microsoft Dynamics 365 Business Central.

  • Difference Between Passing By Value Or By Reference
  • Integer Difference Example
  • Record Difference Example
  • Reference Data Types

Difference Between Passing By Value Or By Reference

Passing by reference means that a pointer is created. We can work with this variable (the pointer), change it and the original will be changed as well.

On the contrary, by value means that the original variable won’t be changed. A second variable is created, not a pointer.

Here is a simple but useful image to understand the concept.

The ‘fillCup()’ function will fill the cup on the left and the original cup will be changed. While the one on the right hasn’t been filled.


Integer Difference Example

Let´s see now in detail how it works and how this concept applies in AL.

In this first example, we are passing an integer variable by value.

The message will show ‘1’ and not ‘2’. That is because we have two different variables in two separate functions.

In the following image, we have added ‘var‘ before the integer variable. That´s how we pass a variable by reference in AL.

As we have mentioned before, in the second function, we have a pointer to the same variable. We can say that the variable ‘returns’. And the message will show a ‘2’.

Record Difference Example

A record variable works the same way.

In this example, you can see how ‘Customer’ variable is passed by value. Then, we perform a modify to change the name. Note that the modify is inside the second function. The customer variable has been called by value, which will create a second variable.

If the modify is outside the function, the value won´t be changed. The second ‘customer’ variable won´t communicate with the first one. As we have mentioned, they are two different variables.

We can pass it by reference and now the modify can be done in the first function. The record holds the value ‘Business Central VAR’ because it ‘returned’ to the first function.

Reference Data Types

There are some data types that can only be passed by reference. Even if we don´t specify ‘var‘ before the variable. That´s what a reference data type is.

For dictionaries, when defining a value, it can only be null if you use a reference type. You can see it in the Microsoft screenshot below.

If you want to learn more about dictionaries follow the article below:

Lists are also reference types. As you can see in the Microsoft documentation:

If you want to learn more about lists check the article below:

Other reference types are HTTP, JSON, TextBuilder, and XML types as you can see in the Microsoft documentation:

You can learn more about passing by value or by reference in the following posts:

That´s all. Hope you find it helpful.

Passing By Value Or By Reference In Business Central

Post navigation

Leave a Reply

Your email address will not be published. Required fields are marked *