How to do it...

  1. In Visual Studio Code, open the folder containing the dotnet-interop-start project that you downloaded from GitHub.
  2. 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"
By adding the previous property to your project, your extension will no longer be supported on the Business Central SaaS platform. In other words, you can only use .NET controls in your AL code for on-premises (or possibly private cloud) installations, so be aware of this limitation when designing solutions, in case the on-premises customer has plans to move to the cloud.
  1. 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.

You can use the tdotnet snippet to create a basic dotnet package. It is recommended that you define only one package to hold all the .NET assemblies and types that you want to use.
  1. 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:

  1. 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;

}
  1. 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;
}
}
}
}
  1. 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;
}
}
}
  1. Add a couple of global variables for the calculation, after the actions section:
var
Number: Decimal;
Power: Decimal;
  1. 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:

  1. Use the  icon and search for DotNet Example Page.
  2. Click on it to open the page.
  3. Enter some numbers and choose Process | Calculate power.

You should see a result similar to the following:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.210.143