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.
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.
Normally, with .Net interoperability you would look at the String.Split command. In C# code this would look like this:
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.
So now, the code would be:
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:
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.
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.
Fill this variable with the separator value and call the ToCharArray() function on it, while passing it to the Split() function.
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:
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:
The result (no, I’m not going to post 10 screenprints here… )
Method 3: loop trough it using the Enumerator:
Declare a new DotNet variable of type System.Collections.IEnumerator.
Use this code to get the Enumerator and loop trough the values.
The result is exactly the same, so I’m not going to repost it…
Method 4: Use FOREACH (only NAV 2016 and up)
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.