次の方法で共有


Quick Tip: Remember to check than an event is assigned before calling

 

With NAV 2009 R2 The ControlAddIn event had been introduced. An Add-In can raise this event and the C/AL OnControlAddIn trigger will be called.

When you use the Add-In templates for Visual Studio and create an Add-In that supports this event you might want to remember to do a simple check whether the Event had been subscribed before calling it.

image image

         protected void RaiseControlAddInEvent(int index, string data)
        {
            if (this.ControlAddIn != null)
            {
                this.ControlAddIn(index, data);
            }
        }

        public event ControlAddInEventHandler ControlAddIn;

The method RaiseControlAddInEvent is a sample for this. It will be created when you use some of the Add-In templates for Visual Studio and is already part of some of the base classes for specialized Add-Ins – like the classe StringControlAddInBase.

And when we let an Add-In for Dynamics NAV 2013 expose custom events we will remember to use a similar approach for each individual exposed event!

 

To understand the necessity for this, you should know that the client can decide to subscribe and unsubscribe from the exposed events any time, based on the initialization / de-initialization or other states of a page. The Add-In can simply check whether an event can be received & processed by the client at any time by checking that it is asigned.

 

Thumbs up Thank you, Bert K. (Tectura) for pointing this out! This is certainly something to be aware  of when creating Add-Ins. Thumbs up

 

Christian Abeln

Senior Program Manager

Microsoft Dynamics NAV

Comments

  • Anonymous
    December 08, 2013
    Hey Christian. Another approach instead of always having to check for null is to create your event like so:public event MySampleEvent<MySampleEventArgs> SampleEvent = new delegate { };In nothing is subscribed to the event, the blank delegate is ensures you never have to worry about an null exception. If something is subscribed, it will work as normal. As an added bonus, this approach is thread-safe.