SetGlobalObject

導入

v5.1

詳細

SetGlobalObjectはSetGlobalに似ていますが、JScript 配列や他のオブジェクトを保存するために使用できる点が異なります。 このコマンドを使用して保存されたデータは、GetGlobalを使用して取得できます。

オブジェクトは「参照」保存されるので、その後オブジェクトの内容に追加された変更は、グローバル変数を取得するすべての呼び出し側から見ることができます。

スクリプト構文

SetGlobalObject( in_VariableName, in_Value );

パラメータ

パラメータ タイプ 詳細
in_VariableName 文字列 グローバル変数の名前。
in_Value Variant グローバル変数の値となるオブジェクト

1. JScript の例

/*
        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 の例

/*
        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

関連項目

SetGlobal GetGlobal ArgumentCollection.Add