In X++ programming, there are simple variables types or more complex types. Native types are built-in variable types like int, str, real etc. The simple types and can be used to store specific variable values of type string, number, integer and so on.
There are also more advanced types in X++. One of them is classes. A class is a representation of a real life object like customer, vendor, product, bank, person and more. It can be anything that requires more than a single value like a simple type. In fact, a class uses simple types to store the data in the object. A class can be instantiated to create an instance or object of that class. This object is stored in a variable just like simple types, but there is a fundamental differences between simple types and objects.
The biggest difference between these two types that I want to make clear, is that object variables are references to objects. So, two different variables can point to the same object. If you change something in the object using the first variable, those changes will reflect in the second variable, and visa versa. It is key to understand this concept because in X++, this something that is common in the application.
Code
Two native type variables assigned the same value. One is changed but different values are printed to the output.
public static void main(Args _args) { real len1, len2; len1 = 1.23; len2 = len1; len2 = 2; info(strFmt('len1: %1', len1)); info(strFmt('len2: %1', len2)); } // Generated output: // len2: 2 // len1: 1.23
Two variables pointing to the same object, or referencing the same object. I am using tables in this example to make it a bit simpler. Tables are actually classes and you don't have to instantiate them like classes.
public static void main(Args _args) { CustTable custTable1, custTable2; custTable1.AccountNum = 'C1001'; custTable1.CustGroup = 'ABC'; custTable2 = custTable1; // assign object 1 to 2. object 2 is now referencing 1, both are pointing to the object 1. custTable2.CustGroup = 'XYZ'; // change customer group of object 2, object 1 will also reflect this change because it is pointing to the same object. info(strFmt('custTable1.AccountNum: %1', custTable1.AccountNum)); info(strFmt('custTable1.CustGroup: %1', custTable1.CustGroup)); info(strFmt('custTable2.AccountNum: %1', custTable2.AccountNum)); info(strFmt('custTable2.CustGroup: %1', custTable2.CustGroup)); } // Generated output: // custTable1.AccountNum: C1001 // custTable1.CustGroup: XYZ // custTable2.AccountNum: C1001 // custTable2.CustGroup: XYZ