With .NET Framework interoperability in Microsoft Dynamics NAV objects, you can configure a DotNet variable to subscribe to events that are published by a .NET Framework type. Events are handled by triggers in the C/AL code of the Microsoft Dynamics NAV object. To configure a DotNet variable to subscribe to events published by a .NET Framework class, you set the WithEvents Property of the DotNet variable. A trigger is automatically added in the C/AL code for each event that is exposed by the .NET Framework type. For more information about events in NET Framework Interoperability, see Handling Events.
|  Important | 
|---|
| You can only subscribe to events on a DotNet variable that is defined as global variable. | 
To subscribe to events published by a .NET Framework type
- In the development environment, open the Microsoft Dynamics NAV object that uses .NET Framework interoperability, and then open the C/AL code. 
- On the View menu, choose C/AL Globals. 
- On the Variables tab, select the DotNet variable, and then on the View menu, choose Properties. 
- In the Properties window, set the WithEvents property to Yes. - For each event that is exposed by the .NET Framework class, a blank trigger is added to the C/AL code. The triggers have the following format.  Copy Code Copy Code- DotNetVariable :: Event (sender : Variant;e DotNet "EventArgs") - DotNetVariableis the name of the DotNet variable that subscribes to the event.
- Eventis the name of the event published by the .NET Framework class
- ()will contain any parameters that the event has.
 
- Add code to the event triggers to handle the events. 
Example
The following code example implements a simple synchronous event that will invoke a C/AL trigger without any parameters. You will create a .NET Framework assembly that contains the event, and then subscribe to the event in the C/AL code of a Microsoft Dynamics NAV codeunit.
To create and deploy the assembly that includes an event
- In Visual Studio, create a C# class library project that is called MyEvent. 
- Add the following code that implements the ChangedEvent event in the EventsNoArgs class. - C#  Copy Code Copy Code- namespace MyEvent { public class EventsNoArgs { public delegate void ChangedEventHandler(); public event ChangedEventHandler ChangedEvent; // Invoke the ChangedEvent protected virtual void OnChangedEvent() { if (ChangedEvent != null) { ChangedEvent(); } } // The following method is exposed to C/AL and will invoke the event trigger that is registered in the ChangedEvent variable. public void FireEvent() { OnChangedEvent(); } } }
- Build the project. 
- Copy the MyEvent.dll file to the computer that is running the Microsoft Dynamics NAV development environment. You must copy the assembly to the Add-ins folder of the Microsoft Dynamics NAV Windows client and Microsoft Dynamics NAV Server installation folders. The default path of the Microsoft Dynamics NAV Windows client installation folder is C:\Program Files\Microsoft Dynamics NAV\80\RoleTailored Client or C:\Program Files (x86)\Microsoft Dynamics NAV\80RoleTailored Client. The default path of the Microsoft Dynamics NAV Server installation folder is C:\Program Files\Microsoft Dynamics NAV\80\Service. 
To subscribe to the event in C/AL
- In the development environment, create a new codeunit. 
- Create the following global C/AL variables. - Variable name - DataType - SubType - eventVar - DotNet - MyEvent.EventsNoArgs - counter - Integer 
- Open the properties for the eventVAR variable, and then set the WithEvents Property to Yes. - The - eventVar::ChangedEvent()trigger is added to the C/AL.
- On the - OnRuntrigger, add the following code. Copy Code Copy Code- //Reset the global counter to zero. counter := 0; // Construct the DotNet object that implements the event. eventVar := eventVar.EventsNoArgs(); // Fire the event three times. eventVar.FireEvent(); eventVar.FireEvent(); eventVar.FireEvent(); // Inspect the counter and raise an error if the count does not match the number of events fired. if counter <> 3 then error(‘Unexpected count value, %1’, counter); message(‘Received %1 events’, counter); 
- On the - eventVar::ChangedEvent()trigger, add the following code to increment the counter variable. Copy Code Copy Code- counter += 1; 
- Save and compile the codeunit. 
- To test the event, in Object Designer, select the codeunit, and then choose Run. - The Microsoft Dynamics NAV Windows client opens and the message “Received 3 events” appears. - If you remove one of the - eventVar.FireEvent();statements, and then compile and run the codeunit again, you will get an error message.




