In the first article of this series, I covered the differences in operators between Java and X++. As a Java programmer, some of the other areas of interest in X++ programming include the use of var, attributes, and anytype. Using these data types and decorators effectively can make X++ program very useful. In this blog, I will explain what can and cannot be done with these data types using sample code.
The Var Data Type
Any data type can be assigned to var type. However, once a data type is assigned, it cannot be assigned to a different data type in the same scope or it will produce an error. Java SE 9 has included the use of var in their release. The Java compiler will infer the use of var automatically, similar to the X++ compiler as shown below.
var x1 = 20;
info(“var is now assigned as ” + int2Str(x1));
The Use of Anytype
Java has built-in classes that can take objects of any type and return it at run time. For example, let’s take a look at the Box class. The only thing to consider when writing such code is that the class has defined method for set() and get() to initialize and retrieve attributes respectively, and this class does not operate on primitive types. X++ has much simpler way of accomplishing similar functionality. Additionally, X++ can operate on primitive data types.
The anytype is initialized with any value, but its type is determined at run time. The following code in X++ and its execution explains this:
class Test
{
/// <summary>
/// Runs the class with the specified arguments.
/// </summary>
/// <param name = “_args”>The specified arguments.</param>
public static void main(Args _args)
{
anytype intnumber = 20;
info(“Right now anytype is assigned an integer value of ” + int2str(intnumber));
anytype stringa = “Hello”;
info(“Right now anytype is assigned a string value of ” + stringa);
}
}
The Use of Attributes in X++
The use of attributes is about supporting readability in X++. Before getting to the start of a method in X++, attributes represent a good way to decorate a class. There are quite a few attributes that are available in X++.
For our examples, we’ll discuss IsObsolete.
If IsObsolete is used at the beginning of a method, it will print a warning to the programmer telling that this function is obsolete. I can see this feature is backward compatible for X++ programmers. The following sample code illustrates the use of attribute ExtensionOf, which tells the compiler that the class that follows in the following line is an extension of MyClass.
Want to learn more? Visit academy.rsmus.com for eLearning courses and information about our hosted training classes in Denver! Or Contact our Microsoft Dynamics experts at RSM (855) 437-7201.
By: Altaf Siddiqui