The everyday life of a Dynamics AX developer
In part 1, we talked about the use of a protected new method and a static constructor, and how it helps improve your classes.
Now let’s see how it helps you maintain you code better when extending classes.
In the previous example, we printed some general information about a record to the screen.
Let’s say that, in case of CustTable we want to have information printed to to screen that is specific to this record, like the AccountNum or Name field.
We will need to create a new class that extends the class we created earlier.
1. ClassDeclaration KLFShowRecordInfo_CustTable
In the ClassDeclaration, we include the keyword ‘extends’ to specify that our new class extends the functionality of the class KLFShowRecordInfo.
2. Construct method KLFShowRecordInfo_CustTable
We will also add a construct method to this class. This will create an instance of this class for us, and set the parm method.
Because we extend a class that contains this parm method, we don’t need to add it to this class again.
3. Run method KLFShowRecordInfo_CustTable
In the run method, we print data from the CustTable record to screen.
You could call the super() method if you want the general information to be printed as well.
4. Construct method KLFShowRecordInfo
The only thing we need to change to the existing class is the construct method.
Modify it like this:
The switch will check which table is being processed, and will create an instance of KLFShowRecordInfo_CustTable in case of CustTable. For all other tables, it will construct an instance of KLFShowRecordInfo.
Let’s test it:
This concludes my articles about the protected new and construct methods. I realise that the examples are very simple, but the principle will remain the same, even in more complex scenarios.
To sum up:
My basic recommendations when creating classes are:
- Use a protected new method
- Use a construct method
- Use parm methods
- Create a run method that contains your business logic
- Instead of modifying existing classes, create new ones that extend existing
If you disagree, be sure to leave a comment :).