Creating multiple variables to store related information can be time consuming. It leads to more code and more work. Using an array to store related and similar types of information can speed up development and lead to much more manageable code. This recipe will show you how to create and access array elements.
Let's create a new codeunit from Object Designer.
Add the following global variables by navigating to View | C/AL Globals (Alt + V + B):
Name
Type
i
Integer
IntArray
Integer
Now, with the cursor on the
IntArray
variable, navigate to View | Properties (Shift + F4).In the Property window, set the following property:
Property
Value
Dimensions
10
Write the following code into the
OnRun
trigger of the codeunit:FOR i := 1 TO ARRAYLEN(IntArray) DO BEGIN IntArray[i] := i; MESSAGE('IntArray[%1] = %2', i, IntArray[i]); END;
To complete the task, save and close the codeunit.
On executing the codeunit, you should see a window similar to the following screenshot:
An array is a single variable that holds multiple values. The values are accessed using an integer index. The index is passed within square brackets ([
]
).
Note
NAV provides several functions to work with arrays. For instance, ARRAYLEN
returns the number of dimensions of the array and COPYARRAY
will copy all of the values from one array into a new array variable. You can find a complete list of the array functions in the Developer and IT Pro Help option in the Help menu of Microsoft NAV Development Environment.
Open the 365
, Format Address
codeunit. Notice that the first function, FormatAddr
, has a parameter that is an array. This is the basic function that all of the address formats use. It is rather long, so we will discuss only a few parts of it here.
This first section determines how the address should be presented based on the country of the user. Variables are initialized depending on which line of the address should carry certain information. These variables will be the indexes of our array.
CASE Country."Contact Address Format" OF Country."Contact Address Format"::First: BEGIN NameLineNo := 2; Name2LineNo := 3; ContLineNo := 1; AddrLineNo := 4; Addr2LineNo := 5; PostCodeCityLineNo := 6; CountyLineNo := 7; CountryLineNo := 8; END;
Then we will fill in the array values in the following manner:
AddrArray[NameLineNo] := Name; AddrArray[Name2LineNo] := Name2; AddrArray[AddrLineNo] := Addr; AddrArray[Addr2LineNo] := Addr2;
Scroll down and take a look at all of the other functions. You'll see that they all take in an array as the first parameter. It is always a text array of length 90
with eight dimensions. These are the functions you will call when you want to format an address. To use this codeunit correctly, we will need to create an empty array with the specifications listed before and pass it to the correct function. Our array will be populated with the appropriately formatted address data.
Manipulating string contents
The Using the CASE statement to test multiple conditions recipe in Chapter 2, General Development