Creating multiple variables to store related information can be time consuming. It leads to more code and hence, more work. Using an array to store related and similar type 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.
Create a new codeunit in Object Designer.
Add the following global variables:
Name
Type
i
Integer
IntArray
Integer
With the cursor on that variable, click on View | Properties ( Shift +F4).
Set the following property:
Property
Value
Dimensions
10
In the
OnRun
trigger add the following code:FOR i := 1 TO ARRAYLEN(IntArray) DO BEGIN IntArray[i] := i; MESSAGE('IntArray[%1] = %2', i, IntArray[i]); END;
When you run the codeunit you will see ten windows, one after the other, 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 ([]).
NAV provides several functions to work with arrays. ARRAYLEN
returns the number of dimensions of the array. COPYARRAY
will copy all of the values from one array into a new array variable. For a complete list of functions, search the C/SIDE Reference Guide under the Help menu for "Array Functions".
Open codeunit 365, Format Address. Notice the first function, FormatAddr
, has a parameter which 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 certain information appear. The variables will be the indexes of our array.
CASE Country."Contact Address Format" OF arrayaddress creating, Format Address usedCountry."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 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 8 dimensions. These are the functions you will call when you want to format an address. To use this codeunit correctly, you will need to create an empty array with the specifications listed before and pass it to the correct function. Your array will be populated with the appropriately formatted address data.