Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Microsoft Dynamics NAV 2009 Programming Cookbook

You're reading from   Microsoft Dynamics NAV 2009 Programming Cookbook Build better business applications with NAV

Arrow left icon
Product type Paperback
Published in Oct 2010
Publisher Packt
ISBN-13 9781849680943
Length 356 pages
Edition 1st Edition
Arrow right icon
Author (1):
Arrow left icon
Matthew Traxinger Matthew Traxinger
Author Profile Icon Matthew Traxinger
Matthew Traxinger
Arrow right icon
View More author details
Toc

Table of Contents (18) Chapters Close

Microsoft Dynamics NAV 2009 Programming Cookbook
Credits
About the author
About the reviewer
1. Preface
1. Strings, Dates, and Other Data Types FREE CHAPTER 2. General Development 3. Working with Tables and Records 4. Designing Forms 5. Report Design 6. Diagnosing Code Problems 7. Roles and Security 8. Leveraging Microsoft Office 9. OS Interaction 10. Integration 11. Working with SQL Server 12. The RoleTailored Client Index

Repeating code using a loop


Looping is an essential part of dealing with records in NAV. Using a FOR loop is a common way to iterate over multiple lines of code. This recipe will show you how to construct a FOR loop and use it.

How to do it...

  1. Create a new codeunit from Object Designer.

  2. Add the following global variables:

    Name

    Type

    n

    Integer

    i

    Integer

    Factorial

    Integer

  3. Add the following code to the OnRun trigger of the codeunit:

    Factorial := 1;
    n := 4;
    FOR i := 1 TO n DO
    Factorial := Factorial * i;
    MESSAGE('Factorial of %1 = %2', n, Factorial);
    
  4. Save and close the codeunit.

  5. When you run the codeunit you will see a window similar to the following screenshot:

How it works...

A FOR loop has four parts: a counter, a starting value, the step to be taken, and an ending value. In this code, our counter variable is "i". The starting value is 1 and the ending value is "n", which in this case has been assigned to the value 4.

Each time the loop iterates, the value of "i" is increased by one ...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image