Exercise - Create an interface
In this exercise, you're a developer who works for CRONUS International Ltd. You want to create and implement an interface, IAddressProvider, which has one method getAddress with a certain signature.
Tasks
Create an interface.
Create two codeunits that implement the interface.
Create a page with an action that tests the interface.
Create an enum to support the interface initialization.
Steps
Follow these steps to complete the exercise:
Create a new .al file with following code that defines an interface IAddressProvider, which has one method getAddress with a certain signature:
interface IAddressProvider { procedure GetAddress(): Text; }
Create two new .al files to create two codeunits: CompanyAddressProvider and PrivateAddressProvider. Both codeunits will implement the IAddressProvider interface, and each will define a different implementation of the getAddress method, which in this case is a simple variation of address value:
codeunit 65100 CompanyAddressProvider implements IAddressProvider { procedure GetAddress(): Text; begin exit('Company address \ Denmark 2800') end; }
codeunit 65101 PrivateAddressProvider implements IAddressProvider { procedure GetAddress(): Text; begin exit('My Home address \ Denmark 2800') end; }
Create a new .al file to create a page with an action that captures the choice of address and calls, based on that choice, an implementation of the IAddressProvider interface:
page 65100 MyAddress { PageType = Card; ApplicationArea = All; UsageCategory = Administration; layout { area(Content) { group(GroupName) { } } } actions { area(Processing) { action(WhatsTheAddress) { ApplicationArea = All; Caption = 'What's the Address?'; ToolTip = 'Select the address.'; Image = Addresses; trigger OnAction() var iAddressProvider: Interface IAddressProvider; begin AddressproviderFactory(iAddressProvider); Message(iAddressProvider.GetAddress()); end; } action(SendToHome) { ApplicationArea = All; Image = Home; Caption = 'Send to Home.'; ToolTip = 'Set the interface implementation to Home.'; trigger OnAction() begin sendTo := sendTo::Private end; } action(SendToWork) { Image = WorkCenter; Caption = 'Send to Work.'; ToolTip = 'Set the interface implementation to Work.'; ApplicationArea = All; trigger OnAction() begin sendTo := sendTo::Company end; } } } local procedure AddressproviderFactory(var iAddressProvider: Interface IAddressProvider) var CompanyAddressProvider: Codeunit CompanyAddressProvider; PrivateAddressProvider: Codeunit PrivateAddressProvider; begin if sendTo = sendTo::Company then iAddressProvider := CompanyAddressProvider; if sendTo = sendTo::Private then iAddressProvider := PrivateAddressProvider; end; var sendTo: enum SendTo; }
Create a new .al file to create an enum that holds two values: Company and Private.
enum 65100 SendTo { Extensible = true; value(0; Company) { } value(1; Private) { } }
Publish your extension and open the MyAddress page.
Select the Send to Home action.
Select the What's the Address action and verify that the result resembles the following screenshot.
Select the Send to Work action.
Select the What's the Address action and verify that the result resembles the following screenshot.