Introduction To X++
Introduction To X++
Introduction
X++ is the primary programming language used in the MorphX Development
environment. When you develop with X++, refer to Microsoft Dynamics AX
Developer Help, available from the Help menu for detailed information. The
following are important features of X++:
1-1
Development II in Microsoft Dynamics® AX 2012
Characteristics of X++
The following are characteristics of X++:
Scenario
Systems developers in a large development environment have joined a team that
is customizing a Microsoft Dynamics AX application. This is the team's first
project in X++ so they need to learn what development tools are available to
them within Microsoft Dynamics AX.
Development Tools
All elements that comprise the Microsoft Dynamics AX application (classes,
forms, tables, and more) are organized in the Application Object Tree (AOT).
This is a central, organized index to all application elements, displayed in a
graphical tree view.
As developers create new projects or modify existing projects, they use a series
of development tools. These tools include the following:
• X++ Editor
• X++ Compiler
• X++ Debugger
• Visual Studio
• Visual Studio Debugger
1-2
Chapter 1: Introduction to X++
Other tools exist to aid developers while writing code and for managing
modifications they make to elements in the AOT.
All these tools, apart from Visual Studio, are accessible from the Development
Workspace. This is a unique version of the regular workspace, designed
specifically for developers to work in. To launch a development workspace press
Ctrl+Shift+W from the regular workspace. Alternatively, a command line
parameter (-development) can be added to the Microsoft Dynamics AX client
shortcut, to automatically launch in the development workspace. Development
cannot be done in the regular workspace, so a development workspace must be
opened before doing any development.
There are several toolbar buttons in the heading of the X++ editor window.
Many of these functions can be started using keyboard shortcuts. Moving the
pointer over these buttons reveals the button's function. These buttons are
described in the following table:
1-3
Development II in Microsoft Dynamics® AX 2012
1-4
Chapter 1: Introduction to X++
To make reading easier, the X++ editor color-codes different kinds of code as
follows:
Jobs
A job is a stand-alone block of code in Microsoft Dynamics AX that can be run
from the X++ editor. Jobs are used primarily for testing code during development
and running batch processes within Microsoft Dynamics AX that affect large
amounts of data.
This course uses jobs as a mechanism to write code and run it directly from the
X++ editor. Most code for applications is written in classes, methods, tables, and
forms. However, jobs are useful for testing blocks of code before inserting them
into methods or classes.
The following is default code when you create a new job in the AOT:
NOTE: Unlike classes and other elements in the AOT, jobs can only be renamed
in the Code Editor. Most other AOT elements can have their name changed in the
Property window, or directly on the AOT node.
Create a new job by right-clicking the Job node on the AOT, and selecting New
Job. The X++ editor opens, displaying the new job. The job can be renamed in
the text editor or in the AOT. In the example above, the job is called Job1 and
can be renamed by change Job1 to the required namein the text editor.
1-5
Development II in Microsoft Dynamics® AX 2012
Compiler
When code that is written in X++ is ready to be executed, use the compiler. The
compiler is a translation program that converts X++ into a language that can be
interpreted.
Code can be compiled by clicking the Compile button in the X++ editor toolbar
or by pressing F7. It can also be compiled by right-clicking the node in the AOT
and selecting Compile.
In this course you will also learn about inheritance. Classes that inherit from, or
extend, other classes can be compiled by right-clicking the main class, then
selecting Add-Ins > Compile forward. This means any changes that are made to
the main class are propagated down to the sub-classes correctly.
Use the Compiler Output window to view information to help improve and
correct errors in the code. The Output window will update its contents every
time a compilation is performed.
1-6
Chapter 1: Introduction to X++
• Filter buttons: At the top of the window, there are buttons to toggle
the four different compile result types on and off. Toggling the result
type off will remove results of that type from the Compiler Output
window's list.
• Reset button: This button will reset the contents of the Compiler
Output window.
• Edit button: This button will open the X++ editor or Property sheet
corresponding to the selected compile result.
• Import/Export button: This button allows compile results to be
exported to an HTML file, or imported into the Compiler Output
window from an HTML file.
• Setup button: This button accesses forms where the Compiler
Output window parameters can be set for the user. These include
settings such as the Diagnostic level of the compiler, and which Best
Practices will be enforced.
Debugger
Microsoft Dynamics AX includes a powerful stand-alone debugging tool for
X++ code. Use the Debugger to debug X++ code running on the:
NOTE: Managed code and X++ compiled into Intermediate Language (IL) are
debugged using the Visual Studio debugger. This topic will be covered in the
Visual Studio Integration chapter of the Development III course. This topic only
covers debugging standard X++ code.
1-7
Development II in Microsoft Dynamics® AX 2012
Breakpoints
Breakpoints are set on individual lines of code. This is a command to stop
program execution at that point and enter the debug mode.
When the logic flow of the code in a user's session reaches a breakpoint set by
the same user, the debug session begins and the debugger is automatically
started.
To set a breakpoint:
1. Rest the cursor within the X++ code in the editor where you want to
insert the breakpoint.
2. Click the Breakpoint button in the toolbar or press F9.
3. Click in the gray area to the left of the line of code.
Breakpoints can also be set by inserting the keyword Breakpoint into the code.
This should be used with caution, because it will mean that all users will enter the
debugger when the code reaches this point.
All these methods of settings breakpoints are shown in the following figure:
1-8
Chapter 1: Introduction to X++
To turn debugging off, you can visit Tools > Options, and select the Developer
tab. In the Debug field, select None. When you set a breakpoint in code, this field
will automatically be set back to When Breakpoint.
Breakpoints are only recognized on the tiers (client or server) that have been
configured to recognize them. Code running on the client tier will always stop at
a breakpoint in a user's session, when the user has debugging turned on. Code
running on the server tier will only stop at a breakpoint when the AOS has
specifically been configured to enable breakpoints. This is a setting on the
Microsoft Dynamics AX Server Configuration Utility.
The Debugger can display up to six information windows. The windows display
detailed information about the current state of the executing code while you are
debugging. The information windows available are as follows:
Window Description
Code Displays the X++ code currently being debugged. The red
dot indicates where the breakpoint is inserted. The yellow
arrow indicates the line that is being executed. The arrow
can be dragged to a different line of code so that code is
either re-executed or skipped.
Variables Displays the value of the variables. Modified variables
appear in different colors to indicate change. Users can alter
the value of variables in the debugger to see how the
program would run under different conditions.
Call Stack Indicates which method is currently being debugged.
Double-clicking a method further down the stack will take
the Code window to that method.
Watch Contains a user-defined range of variables. The variables
can be dragged and dropped from the Variables window or
the Code window.
Breakpoints Displays the list of currently defined breakpoints with their
enabled status and line number.
Output Displays separate views of text written to the window from
X++ code and kernel code. These views organize
information sent to the Output window.
1-9
Development II in Microsoft Dynamics® AX 2012
Use the toolbar located above the Output window to navigate through the code
and perform other actions.
Moving the pointer over these buttons reveals the button's function and a
keyboard shortcut.
The more generally used buttons are described in the following table, together
with the keyboard shortcut:
1-10
Chapter 1: Introduction to X++
7. Click Apply
8. Click Yes to restart the AOS service. This may take one or two
minutes.
9. Click OK to close the Server Configuration Utility.
10. Restart the Microsoft Dynamics AX client.
Compare
Use the compare tool to highlight differences between versions of a single
element:
1-11
Development II in Microsoft Dynamics® AX 2012
The most common use is to review differences between two layers. This is useful
when determining what was changed in a standard element. To compare two
layers, right-click the element and select Compare. Select which two layers to
compare and then click Compare.
Any element nodes that contain differences will be listed in the lower-left pane,
and the differences can be seen in the lower-right pane. The differences are color
coded to make it clear which version the differences come from.
You can also use the compare tool to move code and element properties between
layers. When a difference is found, a black arrow is displayed in the left margin
of the lower-right pane. The icon will delete code from the current layer, or paste
code in to the current layer. This feature is useful when upgrading code.
NOTE: You can view code in a different layer by right-clicking the element and
selecting Layers.
1-12
Chapter 1: Introduction to X++
X++ Attributes
Microsoft Dynamics AX supports attributes being assigned to X++ code. This
allows for a rich set of metadata to be built. It describes methods and types that
are defined within X++ code.
Attributes are defined as classes that are derived from the SysAttribute class.
The following code for the SampleAttribute class is an example of an attribute
class:
Attributes can then be used on other classes and methods. The following example
shows the structure of a new Attribute class, AuthorAttribute:
1-13
Development II in Microsoft Dynamics® AX 2012
[AuthorAttribute("Isaac")]
public class MyClass()
{
...
}
[AuthorAttribute("Isaac")]
void MyMethod()
{
...
}
1-14
Chapter 1: Introduction to X++
The browser can also be used to explore the hierarchy farther down the tree. In
the tree view on the left-hand pane, expand the selected node to display all
elements that inherit it. In this example, you can expand the CustAccount node,
to display all extended data types that inherit it. The following figure shows the
same browser shown earlier in this topic. The difference now is that the
CustInvoiceAccount type is selected:
1-15
Development II in Microsoft Dynamics® AX 2012
This tool can also be used on other elements, such as classes. When browsing the
hierarchy on classes, the blue boxes in the grid represent a method that is
overridden, or that exists only, on that class. The following figure shows a view
of the hierarchy for the SalesLineType_Sales class:
Open the Type Hierarchy Context window by navigating to: Tools > Type
Hierarchy Context.
With the Type Hierarchy Context window open, select a few different nodes in
the AOT, and notice how the window changes its contents based on the selected
node.
1-16
Chapter 1: Introduction to X++
The following figure shows a view of the Type Hierarchy Context window,
with the SalesLineType_Sales class node selected in the AOT:
Each of the nodes in the Type Hierarchy Context window is also a hyperlink
that will open the Type Hierarchy Browser for an element.
Reverse Engineering
Reverse engineering is used by partners to easily retrieve detailed information
about the structures and relationships of the Microsoft Dynamics AX business
logic and data model. The goals of reverse engineering are to extract
relationships, and integrate and view collections in Microsoft Office Visio as
UML diagrams.
The feature handles both reverse engineering of the data model and the object
model.
You can use the Reverse Engineering tool to visualize data and object models
generated from projects in Microsoft Dynamics AX. These models are created as
Microsoft Visio documents.
To open the Reverse Engineering tool, right-click a project and select Add- ins >
Reverse engineer.
1-17
Development II in Microsoft Dynamics® AX 2012
1. Create a project.
2. Drag AOT elements into the project you want to reverse engineer
into an object model.
3. Right-click the project and select Add- ins > Reverse engineer.
4. Click Visio UML Object model to create a UML object model.
5. Select a name and path for the file and then click OK.
6. From Microsoft Visio, drag object nodes into the center grid to lay
out the desired model.
1. Create a project.
2. Drag the AOT elements into the project you want to reverse engineer
into a data model.
3. Right-click the project and select Add-ins > Reverse engineer.
4. Click Visio UML Data model to create a UML data model.
5. Select a name and path for the file and then click OK.
6. From Microsoft Visio drag table nodes into the center grid to lay out
the desired model.
1-18
Chapter 1: Introduction to X++
Best Practices
Best practices involve understanding all the things that you should do and
distinguishing them from the things that you can do.
Naming Conventions
Naming conventions contribute to consistency and to making the application
easier to understand.
Examples:
• CustJournalPrintOutInvoice
• PriceDiscAdmDelete
• PriceDiscAdmSearch
• PriceDiscAdmName
• PriceDiscAdmTrans
1-19
Development II in Microsoft Dynamics® AX 2012
Additionally:
Code Placement
The placement of code depends on the code you are writing. The preferred
procedure in an object-oriented programming language is to place code in a class
that can be reused and called when it is needed.
You can place code in classes to have better control over where the code is
executed. Performance considerations frequently dictate where code should run,
and so controlling this is desirable. Code included in forms always runs on the
client, and this could cause excessive client/server communication where it might
not be necessary.
BEST PRACTICE: Always place the code nearest to the source so that it can be
more easily manipulated. For example, code that manipulates a table should be
placed in that table's methods.
1-20
Chapter 1: Introduction to X++
Comments
A best practice for all programming is to consistently use comments. It is
important when developing X++ programs in Microsoft Dynamics AX to use
comments and document what the code does and how parameters are used. It is
also good to know who made the change and why. When upgrading code, you
have to decide whether to merge code and if this is the case, how to merge it;
comments are useful to help determine why a code change is made. Note the
following about how to use comments:
/// <summary>
/// Comment where XML tags distinguish sections.
/// </summary>
1-21
Development II in Microsoft Dynamics® AX 2012
The client wants you to demonstrate how to print a message to the screen. Create
a job that prints a message to the screen.
Challenge Yourself!
Create a job that prints the message "Microsoft Dynamics AX is fantastic."
Step by Step
1-22
Chapter 1: Introduction to X++
The client wants you to verify the code executes without errors. Debug the job
that you created in the previous lab.
Challenge Yourself!
Set a breakpoint in the code in the job from the previous lab. Run the code and
step through it.
Step by Step
NOTE: You should enter the Microsoft Dynamics AX Debugger at this point. If
not, ask your instructor.
4. Step through the code by using the icon on the toolbar or F11.
1-23
Development II in Microsoft Dynamics® AX 2012
You have been asked to evaluate the Project Accounting data model to prepare
for required customizations according to customer requirements.
Challenge Yourself!
Make the data model for the Project Accounting module in Microsoft Visio by
using the Reverse Engineering tool in Microsoft Dynamics AX.
Step by Step
1-24
Chapter 1: Introduction to X++
You have been asked to create developer documentation for the Credit Limit
modifications made in the standard Microsoft Dynamics AX application.
Challenge Yourself!
Create an XML file by using the XML documentation generation, for the Credit
Limit development project.
Step by Step
1-25
Development II in Microsoft Dynamics® AX 2012
Summary
This lesson introduced some development tools that you will use to make
modifications to Microsoft Dynamics AX. You learned how to use the X++
editor to write code; how to compile code and review errors with the Compiler;
and how to create breakpoints and step through executing code with the
Debugger. Other tools like the Compare, Type Hierarchy Browser, Type
Hierarchy Context, and Reverse Engineering tools were also introduced.
The next lesson will show how to use these tools by creating a Calculator Class
that uses four basic methods.
1-26
Chapter 1: Introduction to X++
1-27
Development II in Microsoft Dynamics® AX 2012
1.
2.
3.
1-28
Chapter 1: Introduction to X++
Solutions
Test Your Knowledge
1. What are the three main Object-Oriented Programming components?
MODEL ANSWER:
Classes are blueprints that describe the objects derived from them. A class is
a model classification of the methods and variables in a specific type of
object. Objects are instances of classes. Each instance has data members and
logic (methods) defined in the class. Methods are functions (subroutines)
associated with a class or an object. An object implements its behavior with
methods.
MODEL ANSWER:
The Code window pane displays the X++ code that is currently being
debugged. The Variables window displays the value of the variables in the
current block of code. Modified variables appear in different colors to
indicate change. The Call Stack window tells you which method is currently
being worked on. The Watch Status window displays a user-defined range of
variables. The Breakpoint window consists of a header with two columns and
the list of currently defined breakpoints. The Output window in the Microsoft
Dynamics AX Debugger has separate views that display text written to the
window from X++ code and kernel code. These views organize the
information that is sent to the Output window.
MODEL ANSWER:
The X++ editor is used for editing and creating X++ code. This editor
contains many functions in the toolbar, through keyboard shortcuts, and also
by using the context (right-click) menu. The X++ editor has two panes: the
left side shows a list of the current methods and the right side shows the
current X++ code.
1-29
Development II in Microsoft Dynamics® AX 2012
1-30