RSS

Tag Archives: C#

Getting Familiar with X++

In this post, I will make you familiar with the X++ language which is used in Dynamics AX for development. As I have mentioned in my previous post that X++ is quite similar to C#, so all of you have experience of C# won’t have problem in understanding X++. The language is based on Object Oriented Programming principles. You can define access specifiers like Public, Private, & Protected for encapsulation. You can inherit objects, and define polymorphic behavior on methods. I will discuss further in my future post.

So, lets start with the data types that X++ supports. Following are the Primitive data types of X++,

• String (str)

• Integer (int)

• Real (real)

• Boolean (boolean)

• Date (date)

• Enum (enum)

• TimeOfDay (timeofday)

• UtcDateTime (utcdatetime)

• Anytype (anytype)

So these are the primitive data types with the keyword used for it. For example, if you want to declare a variable of type string, you can declare it like that,

str varString;

Need to know little more, Okie, lets make a very small program to understand. In the AOT, there is a Job node. You can create a Job for testing the code. A job yypically holds small X++ programs that are used to test new code. Just right click the Job node and Create new job.

Now write the following codes,

static void Datatypes_string1(Args _args)

{

str 7 fixedLengthString;

str dynamicLengthString;

;

fixedLengthString = “Welcome to Carz Inc”;

dynamicLengthString = “Welcome to Carz Inc”;

print fixedLengthString;

print dynamicLengthString;

pause;

}

strfmt function is used to get the formatted string. Following method is used to format a string and change any occurrences of %n with parameter n.

To illustrate this, the next Job will print 100: Welcome to Carz Inc

static void Datatypes_string_strmft(Args _args)

{

str name;

int a;

;

name = “Carz Inc”;

a = 100;

print strfmt(“%1: Welcome to %2”, a, name);

pause;

}

Notice that the strfmt function also converts any other data types into string.

Let me describe a little about “anytype” . The anytype data type can contain any of the other primitive data types. The type of the variable data is decided by the first value that is set for the variable.

If you expand “Data Dictionary” node you will see a node named “Extended Data Type”. All the extended data types are based on Primitive data type, however the extended data type has more flavor to the data type like stringsize, label and help text or the configuration key to make the data type specific to configuration settings.

Apart from primitive data types there are Composite data types. These are data types where each variable can consist of multiple other variables.

The different kind of composite data types are:

• Container

• Class

• Table

• Array

Each of the above will be discussed later.

Now, lets talk about the syntax of different statement that controls the flow of the program. I will not go much deep and will just mention the syntax of each statement.

For loop

for (initialization; condition; increment)

{

//statements that need repetition that will repeat until the condition is true

}

While loop

while (condition)

{

//statements that need repetition that will repeat until the condition is true

}

Do-while loop

do

{

//statements that need repetition that will repeat until the condition is true

} while (condition)

If, else if, else

if (condtion1)

{

//statements that will run if condition 1 is true.

}

else if (condition2)

{

//statements that will run if condition 2 is true.

}

else

{

//statements that will run if condition 1 and condtion2 is false.

}

switch

switch (variable to match any case)

{

Case 1 :

//statement if case 1

break;

case 2 :

//statement if case 2

break;

case 3 :

//statement if case 3

break;

default

//statement if none of the cases match

break;

}

Note : the case statement can be written string, enum, int etc depends upon the type of variable used in switch.

You can test all the statements by creating new job in AOT and observe how the statements behave and to be well aware of writing code in X++.

 
3 Comments

Posted by on March 23, 2010 in Introduction

 

Tags: , , , ,