Using Collections

In Microsoft Dynamics NAV, a common development task is to write code that loops through a set of records and performs an operation on each record in the set. You can do the same task in the .NET Framework with a collection, which is a .NET Framework object that you can iterate over. To loop through a collection in Microsoft Dynamics NAV, the .NET Framework object must support the System.Collections interface. Many types in the .NET Framework and assemblies that inherit from .NET Framework types support the System.Collections interface. The following example shows how you can write C/AL code to handle collections for a type that supports the System.Collections interface.

Example

The following example implements the System.ArrayList interface and uses the GetProcesses method from the System.Diagnostics.Process type to display a list of process IDs in message boxes. Although this is not a typical example for an ERP product such as Microsoft Dynamics NAV, it shows the flexibility that you get with the .NET Framework.

The example requires that you define the following C/AL variables.

Variable name DataType SubType

MyProcessList

DotNet

'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Array

Process

DotNet

'System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Diagnostics.Process

I

Integer

MyProcessList := Process.GetProcesses();
FOR I:=0 TO (MyProcessList.Length()-1) DO
BEGIN
    Process := MyProcessList.GetValue(I);
    MESSAGE(FORMAT(Process.Id));
END;