Monday, June 20, 2022

Interfaces in Dynamics 365 FO

Interfaces Overview in AX 2012

You create an interface under the Classes node of the AOT. Defining an interface is similar to creating a new class. An interface definition has two components: the declaration and the body.

X++

interfaceDeclaration

{

    #interfaceBody

}


The interface declaration declares various attributes about the interface, such as its name and whether it extends another interface. The interface body contains the method declarations within the interface as shown in the following example.


X++

interface SysDeleteTables

{

    public void doDeleteCustomerTables();

}





Using an Interface

To use an interface, write a class that implements the interface. When a class implements an interface, the class must provide a method implementation for all of the methods declared within the interface as shown in the following example.

class SysDataImport extends SysDataExpImp implements sysDeleteTables

{

// Supply implementation of methods in the interface.

void doDeleteCustomerTables()

{

// Implement the method.

}

}

A class that implements an interface inherits all the method declarations in that interface. However, the class needs to provide bodies for those methods.





Example of Interface

interface IDrivable

{

    int getSpeed()

    {

    }


    void setSpeed(int newSpeed)

    {

    }

}


class Automobile implements IDrivable

{

    int speed;


    public int getSpeed()

    {

        return speed;

    }


    public void setSpeed(int newSpeed)

    {

        speed = newSpeed;

    }

}


class UseAnAutomobile

{

    void DriveAutomobile()

    {

        IDrivable drivable;

        Automobile myAutomobile = new Automobile();

        str temp;


        myAutomobile = new Automobile();


        if (myAutomobile is IDrivable)

        {

            drivable = myAutomobile;

            drivable.setSpeed(42);

            temp = int2str(drivable.getSpeed());

        }

        else

        {

            temp = "Instance is not an IDrivable.";

        }


        info(temp);

    }

}


Link