Language enhancements 

Along with making X++ completely compile in CIL, several language enhancements and constructs have been added to get X++ closer to C#. The following code summarizes some of the new enhancements in X++:

//using keyword for referencing - just like C# 
using coll = System.Collections;
using System.CodeDom;
class MyMainClass
{
// Declare variables and instantiate them in the class declaration
// granular field access mark them public, private or protected.
//static, constant and read only member variables
static int loop;
private const int constValue = 4;
public readonly str readOnlyValue = "ReadOnly";
CodeComment comment = new CodeComment("Print something");

// static constructor using TypeNew keyword
static void TypeNew()
{
loop = 4;
}
public static void main(Args _args)
{
MyMainClass mainClass = new MyMainClass();
mainClass.myMethod();
}
public void myMethod()
{
coll.ArrayList arrayList = new coll.ArrayList();
// Const or readonly variables change- generate compile error
// constValue = 5;
//readOnlyValue = "I want to change this but i cant";
try
{
info(comment.Text);
//use var keyword
for (var i = 1; i <= loop; i++)
{
arrayList.Add(i);
if (i == 3)
{
throw error("Catch me.");
}
}
}
catch
{
error("something happened in try.");
}
// Finaly keyword
finally
{
info(strFmt("Error happened at %1", arrayList.Count));
}
}
/*Output
Print something
Catch me.
something happen in try.
Error happened at 3
*/
}

The preceding code highlights the following new concepts and keywords in the X++ programming language:

  • The using keyword: Similar to C#, now you can use the using keyword to reference assemblies. The preceding code shows two different ways of how you can use the using keyword. 
  • Instantiate variables in class declaration: As shown in the code, now you can instantiate variables in the class declaration.  
  • Static constructor and member fields: You can now declare member fields as static and create static constructors using the keyword TypeNew. In the example code, an earlier static constructor is used to initialize the value of the variable loop.
  • Const and read-only member fields: Now you can declare member field as const or readonly. Attempting to change these fields throws compile errors. Similar to C#, Const are static by default and  must have a value at compilation time. The ReadOnly field must have a set value, by the time constructor is evaluated and the instance is created.
  • Granular field accessNow a field can explicitly be marked as private/protected/public. Default is protected.
  • The var keyword: Similar to C#, now you can use var keyword in X++. The code example earlier shows i in the for loop declared using the var keyword.
  • Declare anywhere/smaller scope:  Now X++ allows you to declare variables in a smaller scope. The preceding code demonstrates this concept where the variable i  is declared in the scope of the for loop.
  • The finally keyword: You can now use the finally keyword along with the try...catch statements; the finally block is always executed.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset