Exercise - Create an interface

Completed

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:

  1. 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;
    }
    
    
  2. 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;
    }
    
    
  3. 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;
    }
    
    
  4. 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)
        {
        }
    }
    
    
  5. Publish your extension and open the MyAddress page.

    Screenshot of the MyAddress page showing the Actions tab.

  6. Select the Send to Home action.

  7. Select the What's the Address action and verify that the result resembles the following screenshot.

    Screenshot of the My Home address message box.

  8. Select the Send to Work action.

  9. Select the What's the Address action and verify that the result resembles the following screenshot.

    Screenshot of the Company address message box.