Navision C/AL can be quite monotonous to develop in – which is also one of its strengths – strength through simplicity, but sometimes one simply desires some power tools to get past the endless repetitive boiler-plate code.

Microsoft .Net Framework, CLR, COM interop, C# and Lazy Evaluation (aka IEnumerable of T) to the rescue!

The example I’ll show here is probably not the best one. I’ll be showing how to lazily enumerate files on the file system. Navision already has a special virtual table called the file table that serves to enumerate over the file system. Nevertheless, this example will show what you can do, and maybe you’ll prefer my approach, although minimizing dependencies on automations is of course desirable.

So, let’s get started here.

First we define an Navision version of the IEnumerable interface. This is straight-forward and is immensely useful for iteroperability between Navision and automations. It provides a very smooth iteration idiom.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Demo.IO
    using System.Runtime.InteropServices;

    public interface ILinear
        object Current { get; }

        bool MoveNext();

        void Reset();

This simple interface provides a great deal of expressiveness due to its simplicity, generality and broad applicability.

In order to make use of this, an equal implementation class should be defined, it merely wraps around an IEnumerable<object>. This is left as an exercise for the reader.

Why IEnumerable of object you may ask? Because COM interop is not so happy about parametric polymorphism and generic types. That wohn’t be a problem in Navision though. The only problem is the limits around passing data to and from Navision where f.x. text is limited to 1024 characters. – But here you can again utilize the above type to indirectly pass around a large string wrapped in an ILinear.

Lazy Evaluation in C# 2.0 (yes, it’s so yesterday, but laziness never expires!) is our means to efficiently enumerate and traverse arbitrarily large file system trees. Here’s how it’s done.

        public ILinear GetFiles(string path, string pattern, bool recursive)
            var result = (Linear)null;
                result = new Linear(LazyGetFiles(path, pattern, recursive));
            catch (Exception) { }
            return result;

I’ll not show the definition of LazyGetFiles here, but you can find the basic technique many places on the Web, e.g.

I always wrap COM entry-points with exception handling to avoid crashing Navision should anything fail (using the most general exception class here). I found it a good practice to do this, in combination with access to the current exception state of the class, for logging unexpected run-time exceptions inside Navision.

It’s a bit painful to declare variables in Navision (you have to go through UI menus to do this, it’s not possible in pure text – although that may change in future versions of Navision) – the point of which is to say that if it were a bit less painful, maybe even with type-inference, one might instead return a Maybe value but we can use ISCLEAR from within Navision to detect null-values and therefore see if a failure occurred.


About xosfaere

Software Developer
This entry was posted in C/AL, Navision, Software, Technical and tagged , , , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s