Forum Discussion

ChristianW's avatar
ChristianW
Valued Contributor
4 years ago

What is the globals object good for?

Hi all What is the globals object good for we are seeing in all the business rules?    
  • ChristianW's avatar
    4 years ago

    With the globals variable, you can store data or objects globally and transfer it from one process steps to another.

    • For the runtime of a consolidation
      With the globals you can populate a variable or create an object in the first entity you calculate and store it for all other entities of a consolidation so that you don't need to calculate it again.
    • For a cubeview/report
      With the globals you can populate a variable or create an object in a dynamic calc when calculating the first cell and store it for all other cells of the cubeview so that you don't need to calculate it again.

    This is a sample how to count process steps

    Dim aSimpleCounter As Integer
    'lock the process to avoid overrides 
    SyncLock Globals.LockObjectForInitialization
    	'Initialize aSimpleCounter with the stored variable, or initialize it with 0
    	simpleCounter = globals.GetInt32Value("SimpleCounter", 0)
    	'Count it up by 1
    	simpleCounter +=1
    	'Save it to globals
    	globals.SetInt32Value("SimpleCounter", simpleCounter)
    End SyncLock

    You can also store objects like list or dictionaries in globals

    Dim nameToStore As String = "MyObjectName"
    'Definition of a list
    Dim anyObject As list(of string)
    'lock the process to avoid multiple initialisations 
    SyncLock Globals.LockObjectForInitialization
    	'Check if the object is already stored in globals
    	anyObject = globals.GetObject(nameToStore)
    	'If not initialize it
    	If anyObject Is Nothing Then
    		'create and populate the list
    		anyObject = New list(of string)
    		anyObject.add("One")
    		anyObject.add("Stream")
    		anyObject.add("Software")
    		globals.SetObject(nameToStore , anyObject)
    	End If
    End SyncLock	
    'Continue working

    Be careful, many object like list and dictionaries are not supporting mutithreaded (support working in parallel), so you can not use them to add members to the list during a report or a consolidation.

    But there are multithreaded versions you can use for this purpose

    'Additional Imports for concurent list and dictionary
    Imports System.Collections.Concurrent
    'create a concurrent dictionary
    Dim concDict As New ConcurrentDictionary()