Using Global Variables

Global variables are often needed because of flawed implementation designs. However, if used for caching purposes, global variables can provide increases in performance. This topic describes how you can implement a global variable with zero maintenance during an upgrade.

Get the globalCache variable located on the ClassFactory class:
SysGlobalCache globalCache = ClassFactory.globalCache();
Call the set method:
globalCache.set(str  owner , anytype  key , anytype  value );
Parameters
Description
owner
A unique name that identifies you as the user. Use classIdGet(this) or classStr(myClass).
key
Identifies your variable. This is useful if you need more than one global variable from the same location.
value
The actual value of your variable.
Get the globalCache variable, located on the ClassFactory class:
SysGlobalCache globalCache = ClassFactory.globalCache();
Call the get method:
value = globalCache.get(str  owner , anytype  key , anytype  returnValue  = '');
Parameters
Description
owner
Must be a unique name that identifies you as the user.
key
Identifies your variable.
returnValue
The value you want if the global variable has not been set. This is useful for caching purposes. See the following example.

void new(Integer _width  = Imagelist::smallIconWidth(),
         Integer _height = Imagelist::smallIconHeight())
{
    SysGlobalCache globalCache;
    Container      packedData;
    ClassName      className;
    ;
 
    if (this.keepInMemory())
    {
        globalCache = ClassFactory.globalCache();
        className   = classId2Name(ClassIdGet(this));
        packedData  = globalCache.get(className, 0, connull());
        imageList  = globalCache.get(className+classStr(imagelist), 
                                     0, 
                                     null);
    }
    if (!imageList)
    {
        imagelist = new Imagelist(_width,_height);
        this.build();
        if (this.keepInMemory())
        {
            globalCache.set(className, 0, this.pack());
            globalCache.set(className+classStr(imagelist), 
                            0, 
                            imagelist);
        }
    }
    else
    {
        this.unpack(packedData);
    }
}
Global class: 
The Global class contains about 250 default static methods. Each of these methods can be considered an extension to the built-in functions in the X++ language.

Normally, when referring to a static method on a class in X++, you must use the following syntax.
ClassName::methodName(...);
However, the compiler treats the methods on the Global class in a special way. It allows you to omit the reference to the class name. These two lines have the same meaning.
Global::info(...);
info(...);
The info method on the Global class is typically used to send an informational message to the Infolog form.

Comments

Popular posts from this blog

Dynamics Ax 2012 Interview Questions:

Oops concept in AX