X++ in AX7: Static members


You can now declare class member variables as static. The semantics are exactly the same as in C#; namely that all instances of the class will share the member, e.g. if one class sets the value, another class can get it.

Naturally; this should be used with care, but there are some really cool use cases, for example, implementing a singleton pattern in now much cleaner.

Example:

class MyClass
{    
    static MyClass singleton;

    public MyClass getInstance()
    {  
        if (!singleton)
        {
            singleton = new MyClass();
        }
        return singleton;
    }
}     

In previous versions of AX you could achieve similar behavior through the SysGlobalCache classes. The main functional difference between the two is that you can flush the SysGlobalCache classes. This is especially useful during test execution, where the test framework is automatically flushing the caches between each run to avoid state leaking from one test to another.

Static members will not automatically be flushed – you can of course create a flush() method yourself and hook it up to the SysTest::postInvokeTearDown() event.

 

THIS POST APPLIES TO MICROSOFT DYNAMICS AX7 PREVIEW; IS PROVIDED AS-IS AND CONFERS NO RIGHTS.

Comments (8)

  1. Could you please address below queries…..
    What is the use of static variable declarations ?
    does it mean static class member variable can only used in the base class it self or even we can used in derived class?

    1. Static in X++ works the same as static in C#. That is: There is just one instance of the variable for the session. You can still control its visibility using private/protected/public. One example where this is useful is when implementing the singleton pattern.

  2. S.Kuskov says:

    getInstance method in this example must be a static?

  3. Hi

    @BPall:   Maxim's reply is correct. The instance is per session. So it is not shared across AOS instances, and each client connecting to the AOS will have its own instance.

    @Yuri:  In AX7 all X++ code is running on the server, so there is no need for synchronization across client/server,

    Best wishes,

    Michael

  4. Maxim Belugin says:

    JFYI static members in X++ 7 are per session (not global)

  5. Yury says:

    Do you know how is it going to synchronise client and server instances of the class?

  6. BPall says:

    Hi,

    That's interessting stuff! Can you outline the impact of running different AOS instances?

    br

    Beat

Skip to main content