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.
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.
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.
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:
The result:
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.
Nice, and a lot better than the stupid SELECTSTR() function in NAV, which doesn’t support the same value twice.
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.
I figured out that the Assembly is ‘mscorlib’.
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.
Super! Thanks for this. Been playing around with Array and Char.Parse for a while now, growing increasingly frustrated. Simplicity!!!
nice post
Thank you – very useful
Hello! This is a nice article! But you can specify how can we import a file when the separator is CRLF? Thank you!
Don’t know what version you are using. If your version has codeunit 10 Type Helper, then use the CRLF function from it. It’s basically char 13 + 10, so you can also use that as an char array.