Using IEnumerator to iterate through a .NET Framework collection in Dynamics NAV

Kushal Pillay
Navigating 365
Published in
2 min readApr 19, 2019

--

In C/AL, You can use the FOREACH statement to iterate through a .NET Framework collection or array object. It is the recommended approach, and provides a very simple construct for C/AL programmers:

FOREACH <Element> IN <List> DO  
<Statement>

The <List> variable must be a DotNet data type that is set to .NET Framework collection or array type.

In .NET, much of the repetitive constructs we use in our day to day lives as a programmer, like the Whlie Loop/For Loop etc are all implementations of the IEnumerator.

Today, I will show you how you can use the IEnumerator in NAV. Although it is always recommended to use the FOREACH statement, it is useful to know what goes on under the hood.

NavListEnumerator : System.Collections.Generic.IEnumerator`1.'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'NavList: .NET Framework or Collection NavListItem: // Initialize .NET List to a NavList of Type <NavListItem>
NavList := GetListType();
NavListEnumerator := NavList.GetEnumerator();WHILE NavListEnumerator.MoveNext() DO BEGINNavListItem := NavListEnumerator.Current;MESSAGE(FORMAT(NavListItem.Amount));END;

I actually was in need of a way to iterate through a .NET Collection, and thought of this approach to solve the problem, later on, I found out that FOREACH already does this for us. Doesn't hurt to tinker around, always take the red pill ;)

--

--