How to split a string to array with Dynamics NAV

30 Nov

During the preparation of the web services examples, I stumbled upon some .Net interoperability issues in Dynamics NAV. One of these issues was about splitting a string into an array.

There are examples out there, how to do that in Dynamics NAV, using pure C/AL code. Not the most elegant code, though. And I also came across a .Net example which looked very complex.

There must be a more elegant way, I thought! And I found one that I want to share with you. In the upcoming web services examples I will use this, so I don’t have to explain it again.

The problem

Let me first introduce the problem.

Image you have a text variable that contains of a number of values, seperated by a certain character, e.g. a comma. And you want to split this into an array of values.

image

Normally, with .Net interoperability you would look at the String.Split command. In C# code this would look like this:

image

How can we achieve this in C/AL code?

First we need to change MyText into a DotNet type variable of System.String. Then we can call the Split() command on it. And we need a DotNet System.Array variable to store the values in.

image

So now, the code would be:

image

Unfortunately this code is not accepted by the C/AL compiler. You get an error, saying the the function call was ambiguous, when you try to compile this code:

image

According to C/SIDE, the Split function only accepts a char or string array instead of a single string. What is possible in C# is not possible in C/AL.

The solution

Now you might, think that it is easy to create a char array and pass that as an argument to the Split() function. And right you are! But in a different way…

Don’t try to declare a DotNet variable of type System.Array and fill it with a seperator value. You will fail on doing that.

The solution is much more simple and elegant!

Create a second DotNet variable of type System.String.

image

Fill this variable with the separator value and call the ToCharArray() function on it, while passing it to the Split() function.

image

This code will compile nicely and you get an array of values from the Split() method.

Get values from the array

Let’s finally look at how we can get values from this array.

Method 1: directly pick an entry:

image

The result:

image

Surprised that it shows Two instead of One? Remember, everything in .Net is zero based!

Method 2: loop trough it using the Array.Length property:

image

The result (no, I’m not going to post 10 screenprints here… Glimlach)

image

Method 3: loop trough it using the Enumerator:

Declare a new DotNet variable of type System.Collections.IEnumerator.

image

Use this code to get the Enumerator and loop trough the values.

image

The result is exactly the same, so I’m not going to repost it… Glimlach

Method 4: Use FOREACH (only NAV 2016 and up)

image

Value is just a regular Text variable here. And again the result is exactly the same as before.

Why do I show four methods to get the values of an array? Just to show that there are more ways to Rome. Personally, I love the FOREACH in NAV 2016.

Method 1 can be used if you know exactly which entry you need. Method 2 and 3 can be used in Dynamics NAV versions before NAV 2016. And method 4 is the preferably method starting from NAV 2016.

4 thoughts on “How to split a string to array with Dynamics NAV

  1. This is a great help for an integration I’m developing; thanks! But could you specify which .NET Assembly you selected to define those DotNet System variables (System.String & System.Array)? I’m having trouble finding it.

    • Great you find it yourself!
      Best way to find assemblies from the .Net Framework is to search for the class on MSDN. The documentation will also contain the assembly where you can find the class.

Leave a Reply

Your email address will not be published.