Book Image

Microsoft Dynamics NAV 7 Programming Cookbook - Second Edition

Book Image

Microsoft Dynamics NAV 7 Programming Cookbook - Second Edition

Overview of this book

Microsoft Dynamics NAV 7 is a business management solution that helps simplify and streamline highly specialized business processes. Learning NAV programing in NAV 7 gives you the full inside view of an ERP system. Microsoft Dynamics NAV 7 Programming Cookbook covers topics that span a wide range of areas such as integrating the NAV system with other software applications including Microsoft Office, and creating reports to present information from multiple areas of the system,. We will not only learn the essentials of NAV programming, you will also be exposed to the technologies that surround NAV including.NET programming, SQL Server and NAV system administration. Microsoft Dynamics NAV 7 Programming Cookbook is written in a direct, to-the-point style to help you get what you need and continue working in NAV. The first half of the cookbook will help programmers using NAV for the first time, by walking them through the building blocks of writing code and creating objects such as tables, pages, and reports. The second half focuses on using the technologies surrounding NAV to build better solutions. You will learn how to write .NET code that works with the NAV system and how to integrate the system with other software applications such as Microsoft Office or even custom programs. You will learn everything you need to know for developing all types of NAV CSIDE objects, as well as how to integrate and maintain a NAV system.
Table of Contents (20 chapters)
Microsoft Dynamics NAV 7 Programming Cookbook
Credits
About the Author
About the Reviewers
Acknowledgements
www.PacktPub.com
Preface
Index

Creating an array


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.

How to do it...

  1. Let's create a new codeunit from Object Designer.

  2. Add the following global variables by navigating to View | C/AL Globals (Alt + V + B):

    Name

    Type

    i

    Integer

    IntArray

    Integer

  3. Now, with the cursor on the IntArray variable, navigate to View | Properties (Shift + F4).

  4. In the Property window, set the following property:

    Property

    Value

    Dimensions

    10

  5. 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;
  6. To complete the task, save and close the codeunit.

  7. On executing the codeunit, you should see a window similar to the following screenshot:

How it works...

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.

There's more...

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.

See also

  • Manipulating string contents

  • The Using the CASE statement to test multiple conditions recipe in Chapter 2, General Development