Udostępnij za pośrednictwem


Event Terminology and Keywords

Applies To: Microsoft Dynamics AX 2012 R3, Microsoft Dynamics AX 2012 R2, Microsoft Dynamics AX 2012 Feature Pack, Microsoft Dynamics AX 2012

In Microsoft Dynamics AX, you can use the event design pattern to make your X++ code more modular and reusable. The term event is a metaphor that explains how delegates are used.

The Event Metaphor

When something important occurs during a program run, there might be other modules that need to process the occurrence. These important occurrences are called events. When an event occurs, the program tells its notifier for the event that the notifier must send notifications of the event. A notification must be sent to all the event handlers that are subscribers of the notifier. When the program tells its notifier to send the notifications, we call that raising an event.

Gg864037.collapse_all(en-us,AX.60).gifTerms for Events

The following table displays the terms that are used to describe the event metaphor.

Metaphorical term

Description

event

An important occurrence in a program module where additional modules must process the occurrence.

notifier

The program element that sends information about the event to all the event handlers that are subscribed to the notifier.

subscriber

The program functions or methods that are subscribed to an event notifier.

event handler

The methods that subscribe to an event notifier. Only the appropriate kind of methods can be event handlers.

X++ Programming with Delegates

The event design pattern starts when you use the AOT > Classes > MyClass > New > Delegate menu to add a delegate to a class.

Gg864037.collapse_all(en-us,AX.60).gifKeywords used for X++ Programming with Delegates

The following table shows the X++ keywords that describe the use of delegates in X++.

X++ keyword or term

Code

Description

delegate

delegate myDelegate(str _information) {}

The code shows how the delegate looks in the method editor in the MorphX client. The return type is always void, so it is not mentioned in the syntax. No code is allowed inside the {} braces.

eventHandler

myClassInstance.myDelegate += eventHandler(otherClass.myInstanceMethod);

The syntax of the eventHandler keyword might give the impression that eventHandler is an X++ function, but it is not a function. The eventHandler keyword tells the compiler that a method is being subscribed to a delegate.

Subscribe or add a method to a delegate

myClassInstance.myDelegate += eventHandler(OtherClass::aStaticMethod);

The static method OtherClass::aStaticMethod becomes subscribed to the delegate.

Call a delegate

myClassInstance.myDelegate("Hello");

This call to the delegate prompts the delegate to call each method that is subscribed to the delegate. The subscribed methods are called in the same sequence in which they were added to the delegate. One subscribed method must complete before the delegate calls the next method.

See also

X++, C# Comparison: Event

X++ Language Programming Guide

Event Handler Nodes in the AOT

Announcements: New book: "Inside Microsoft Dynamics AX 2012 R3" now available. Get your copy at the MS Press Store.