web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

Add a delegate to a static method

(0) ShareShare
ReportReport
Posted on by

Hi,

Is there a way we can add a delegate to a static method in AX.

Thanks in advance

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Martin Dráb Profile Picture
    237,878 Most Valuable Professional on at
    1. If you mean attaching a pre- or post-method event handler: yes, you can.
    2. If you mean calling a delegate: yes, you can.
    3. If you mean subscribing to a delegate: yes, you can.
    4. If you mean defining a new delegate (e.g. delegate void initialized(){}) inside a method (static or not): no, it isn't possible.
  • Community Member Profile Picture
    on at

    Thanks Martin.

    So does a delegate for a static method need to be declared static as well.

    For example:

    class Example

    {

    delegate static void testDelegate(EventHandlerResult res){}

    static void someMethod()

    {

    -- some code

    // my extension code  

    Example::testDelegate(new EventHandlerResult ());

    }

    }

    Please suggest if this is the right method to call a delegate from a static method. Thanks in advance.

  • Verified answer
    Martin Dráb Profile Picture
    237,878 Most Valuable Professional on at

    You can use static delegates, although it is a bit dangerous design. When you start using static members like this, you must think about implications of multi-threading and potential impact on garbage collection. I strongly recommend you start using objects instead of static methods and static events - it's safer and easier to extend in future. Even using a singleton object is much better than static members.

    But if you have a good reason to do it (and you plan how to avoid problems with the static nature of such a design), you can declare your delegate like static delegate void testDelegate(...).

  • Basheer17 Profile Picture
    2,720 on at

    Yes you should use a static delegate. Initialize EventHandlerResult, pass the variable in delegate as parameter instead of new  EventHandlerResult() to get the results.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Priya_K Profile Picture

Priya_K 4

#1
Martin Dráb Profile Picture

Martin Dráb 4 Most Valuable Professional

#3
Ali Zaidi Profile Picture

Ali Zaidi 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans