A new and tremendously powerful feature was introduced in the Fall Release '16. Now you can extend class instances, including adding state. This is available for any class in the system.
We already know we can extend class types. Which in essence allows us to introduce new methods that consumers of the class can benefit from. That was little more than compiler magic; now we got true class extension capabilities.
Suppose you want to extend the SysUserLogCleanup class. Out-of-the-box this class is deleting records from the SysUserLog table. Let's imagine you want to archive these records before they are deleted.
The SysUserLogCleanup class is a runbase class, so you want to add a check mark control to the dialog, get the result of that check box, act on it in the run method, pack/unpack etc. This example extends the state, and acts on the dialog() and getFromDialog() methods.
Please notice, to be a good citizen, I applied these practices:
- Prefixed the added members and methods. I used "mfp" as prefix. This is important to avoid name clashes with other extensions and future version of the class being extended.
- Hooked up post-method event handlers for the methods needed.
Other interesting aspects:
- This also work for forms.
- It even works for tables – except you cannot add state. Table's don't have a class declaration, so that is fair.
- If you add methods to the Global class, you can reuse them everywhere in X++ as functions, i.e. without specifying a type.
- This way of extending a class will not break the extended class's encapsulation. I.e. you will not have access to any private fields or methods.
THIS POST IS PROVIDED AS-IS; AND CONFERS NO RIGHTS.