- In Visual Studio Code, open the folder containing the dotnet-interop-start project that you downloaded from GitHub.
- The first thing we need to do is configure our project to allow .NET controls to be used in our AL code. In Explorer, select app.json. In the Editor window, add the following property:
"target": "Internal"
- In Explorer, create a new file named DotNet Package.al, and in Editor, create a new dotnet package:
dotnet
{
}
Notice that dotnet is referred to as a package and not an object. This package acts as a container to define all the .NET assemblies and types that you will use in your application.
- Next, we need to define the .NET assembly, and which type within the assembly we will use:
assembly(mscorlib)
{
type(System.Math; MyMathLib) { }
}
In the previous example, we're using the System.Math class, but we've assigned it an alias of MyMathLib. When we make references to it in our AL code, we'll be using the alias.
Initially, the AL compiler only knows about the mscorlib assembly. You are able to tell it about other assemblies though, by adding new paths to the al.assemblyProbingPaths property in the AL Language extension settings in Visual Studio Code:
- Now, let's create a short example. In this example, we will create a page that allows you to enter a number and a power. We'll use the math library to do the calculation (for example, 3 to the power of 4).
In Explorer, create a new file named DotNet Example Page.al and in Editor, create a new page object:
page 50145 "DotNet Example Page"
{
PageType = ListPlus;
ApplicationArea = All;
UsageCategory = Tasks;
}
- Add the layout section to the page:
layout
{
area(Content)
{
group(Group)
{
field(Number; Number)
{
Caption = 'Enter the base number:';
ApplicationArea = All;
}
field(Power; Power)
{
Caption = 'Enter the power to be applied:';
ApplicationArea = All;
}
}
}
}
- Add the actions section to the page, after the layout section:
actions
{
area(Processing)
{
action(ActionName)
{
ApplicationArea = All;
Promoted = true;
PromotedCategory = Process;
PromotedIsBig = true;
Caption = 'Calculate power';
trigger OnAction()
var
MessageTxt: Label '%1 to the power of %2 is %3';
begin
Message(MessageTxt, Number, Power,
CalculatePowerOfNumber(Number, Power));
end;
}
}
}
- Add a couple of global variables for the calculation, after the actions section:
var
Number: Decimal;
Power: Decimal;
- And finally, add the function that will perform the calculation, after the var section:
local procedure CalculatePowerOfNumber(Number: Decimal; Power: Decimal): Decimal
var
Math: DotNet MyMathLib;
begin
exit(Math.Pow(Number, Power));
end;
You can see from the preceding code that to use the .NET library, we must create a DotNet variable type and reference the alias name, MyMathLib, which we created in the dotnet package.
Now, we can try it out! Press F5 to build and publish your application:
- Use the icon and search for DotNet Example Page.
- Click on it to open the page.
- Enter some numbers and choose Process | Calculate power.
You should see a result similar to the following: