SetGlobalObject

Introduced

v5.1

Description

SetGlobalObject is similar to SetGlobal but it can be used to store JScript arrays and other objects. Data stored with this command can be retrieved using GetGlobal.

The object is stored "by-ref" so that any later changes to the content of the object are visible to any caller who retrieves the global variable.

Scripting Syntax

SetGlobalObject( in_VariableName, in_Value );

Parameters

Parameter Type Description
in_VariableName String The name of the global variable
in_Value Variant Object that will be the value of the global variable

Examples

1. JScript Example

/*

	SetGlobal with a JScript array will result in a VBArray.  This is good if 

	you want to pass data between JScript and VBScript, but otherwise use

	SetGlobalObject instead

*/

var jsa = [1,2,3] ;

SetGlobal( "JSArrayToVB", jsa ) ;

var vba = GetGlobal( "JSArrayToVB" ) ;

Application.LogMessage( "Contents of VBArray: " + VBArray( vba ).toArray().toString() ) ;

SetGlobalObject( "JSArray", jsa ) ;

var jsa2 = GetGlobal( "JSArray" ) ;

Application.LogMessage( "Contents of JArray: " + jsa2.toString() ) ;

// Expected output

//INFO : Contents of VBArray: 1,2,3

//INFO : Contents of JArray: 1,2,3

2. JScript Example

/*

	This example demonstrates the difference between SetGlobal and SetGlobalObject

*/

var myobj = new Object() ;

myobj.foo = 5 ;

myobj.bar = "some data" ;

// Remember the object as a global

SetGlobalObject( "globalobj", myobj ) ;

// At any time the object can be retrieved

var myobj2 = GetGlobal( "globalobj" ) ;

Application.LogMessage( "Retrieved global: " + myobj2.foo ) ;

// Both myobj and myobj2 point to the same thing

// so changing myobj2 affects myobj as well

myobj2.foo = 6 ;

Application.LogMessage( "Original object has changed: " + myobj.foo ) ;

// SetGlobal on a JScript object will not work, it will end up storing an empty VBArray

SetGlobal( "damagedglobalobj", myobj ) ;

var myobj3 = GetGlobal( "damagedglobalobj" ) ;

Application.LogMessage( "SetGlobal has destroyed myobj: " + VBArray(myobj3).toArray().length ) ;

// Expected output

//INFO : Retrieved global: 5

//INFO : Original object has changed: 6

//INFO : SetGlobal has destroyed myobj: 0

See Also

SetGlobal GetGlobal ArgumentCollection.Add