What does the construct keyword do when added to a method?

c#, c++, x++

Solution

Construct is not a keyword in X++, this is merely a static method called `construct` that returns an `InventMovement` class. It is used to allow you to create a derived class of a base class without having to know which derived class to create. This is how AX implements the Factory pattern. You will see this pattern used in AX in many places where there are abstract base classes.

`InventMovement` is an abstract base class for many other classes, such as `InventMov_Purch` and `InventMov_Sales`. You can't call new() on an abstract class, so instead of having a switch statement to call either `new InventMov_Purch()` or `new InventMov_Sales()` every time you need to create a `InventMovement` class, you use the `InventMovement::construct()` method to call the correct new() for you.

Problem

The code here is X++. I know very little about it, though I am familiar with C#. MS says its similiar to C++ and C# in syntax. Anyway, I assume the code below is a method. It has "Construct" as a keyword. What is a construct/Constructor method? What does the construct keyword change when applied to the function? Also, am I wrong in assuming the code would create some sort of infinite loop? My assumption is that its a method with a return type of "InventMovement". ``` static InventMovement construct(Common buffer, InventMovSubType subType = InventMovSubType::None, Common childBuffer = null) { InventMovement movement = InventMovement::constructNoThrow(buffer,subType,childBuffer); if (!movement) throw error("@SYS20765"); return movement; } ``` Thanks! Kevin

Original source