X++ in AX7: Extension methods


Have you ever experienced a Microsoft provided class or table missing that vital method that would just make your life easier? If so, you might have been tempted to add it yourself using overlayering. And you surely have paid the upgrade price!

You are about to be pleased. In AX7 X++ supports extension methods, similarly to C#.

Suppose we want to add a fullName method to the DirPersonName table. Here is how you do it, without touching the DirPersonName table. Create this new class:

 

static class MyDirPersonName_Extension
{
    static public PersonName fullName(DirPersonName _person)
    {
        return strFmt('%1 %2 %3', _person.FirstName, _person.MiddleName, _person.LastName);
    }
}

Things to remember:

  1. The class must be postfixed with “_extension”.
  2. The class must be static.
  3. The extension methods must be static.
  4. The type of the first parameter determines which type is extended.

Now you can enjoy your extension method:

while select dirPersonName
{
    info(dirPersonName.fullName());
}

Notice:

  1. When calling extension methods, you don’t provide the first parameter – that gets inferred from the instance’s type.
  2. If the extension method took any additional parameters – they (of course) needs to be provided.
  3. This doesn’t break encapsulation. The extension method only has access to public fields and methods on the type. –

Update – as of Fall release 2016, the syntax is now cleaner – and you can do much more (see here). The above example can be rewritten as:

[ExtensionOf(tableStr(DirPersonName))]
final class MyDirPersonName_Extension
{
    public PersonName fullName()
    {
        return strFmt('%1 %2 %3'this.FirstName, this.MiddleName, this.LastName);
    }
}

THIS POST IS PROVIDED AS-IS AND CONFERS NO RIGHTS.

Comments (14)

  1. Krishna says:

    A good enhancement for the customer. No need to worry while upgardation.

  2. Alex Kwitny says:

    Is there a bug with the latest update to use this on Views? I can’t use this method to create a display method on DirPartyPostalAddressView.

    1. Hi Alex,
      Please log bugs via the normal support channels. I’m not able to handle bugs via my blog.
      Thanks,
      Michael

  3. How can we override form data source method like lookupreference or resolvereference

    1. I’m assuming you mean datasource methods that on an already existing datasource (and not one you’ve added yourself). The datasources exposes a number of events you can subscribe to. If that is not sufficient, you’ll need to overlayer the method – as in AX2012.

  4. BryanS says:

    How do you add new methods to forms via extension? Is there a similar syntax, but passing in the formname as first parameter?

    1. Hi Bryan,
      In AX7 you can create form extensions in the Application Explorer. They are much more powerful than the class extensions in this blog. They enable you to extend a form with new methods, data sources and controls.

      Michael

      1. BryanS says:

        Michael,

        It doesn’t look like form’s extensions created form the AOT can have new methods

      2. Artem Tymofyeyev says:

        Same here, we are not able to add a new extension method to a Form

      3. I stand corrected. As of now form extensions do not support adding methods.

  5. MariusMG says:

    It is quite handy to have posibility to extend not just functional methods but also fields/properties to a class. In .NET there is class called ConditionalWeakTable that allows just that. Do you know if new xpp supports similar scenario? How this is possible in ax without running into memory leaks?

  6. NavidAhsan says:

    Does the compiler check that we don't already have a method "fullName" on the DirPersonName?

    1. Yes – it will also discover if several extensions are adding methods with same name.

Skip to main content