Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

c#

x++

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

like image 606
Kevin Avatar asked Oct 28 '25 10:10

Kevin


1 Answers

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.

like image 174
Jay Hofacker Avatar answered Oct 29 '25 23:10

Jay Hofacker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!