The interface is introduced in Business Central 2020 release wave 1. Its concept is similar to other object-oriented programming languages and It is like a template that makes it easier to reuse code. An interface can contain only abstract methods. If you implement an interface to an object, you must override methods that are inherited from the interface.
How to use an Interface in AL
You can use the “tinterface” snippet to create the basic layout for an interface object in the AL and you can implement an interface by the “implement” keyword to objects (Codeunit, Enum). Follow the below example code.
interface UserTemplate
{
procedure SetNameAndAge(FirstName: Text; LastName: Text; Age: Integer)
procedure GetFullName(): Text;
procedure GetAge(): Integer;
}
interface EmployerTemplate
{
procedure SetEmployerID(ID: Code[10]);
procedure GetEmployerID(): Code[10];
}
codeunit 50102 Employer implements UserTemplate, EmployerTemplate
{
procedure SetNameAndAge(FirstName: Text; LastName: Text; Age: Integer)
begin
EmployerFirstName:= FirstName;
EmployerLastName:= LastName;
EmployerAge:=Age;
end;
procedure GetFullName(): Text
begin
exit(EmployerFirstName+' '+EmployerLastName);
end;
procedure GetAge() :Integer
begin
exit(EmployerAge);
end;
procedure SetEmployerID(ID: Code[10])
begin
EmployerID:=ID;
end;
procedure GetEmployerID() :Code[10]
begin
exit(EmployerID);
end;
Var EmployerFirstName: Text;
EmployerLastName: Text;
EmployerAge: Integer;
EmployerID: Code[10];
}
codeunit 50103 TestRun
{
trigger OnRun()
begin
Employer.SetNameAndAge('Vidura','Dananjaya',24);
Employer.SetEmployerID('EMP50001');
Message(Employer.GetFullName());
Message(Format(Employer.GetAge()));
Message(Employer.GetEmployerID());
end;
var
Employer: Codeunit Employer;
}
Code language: PHP (php)
Visit the previous guide about the Snapshot Debugging.