How can I use the Dictionary object to store a global variable

  • Questions
  • How can I use the Dictionary object to store a global variable
Question ID: 105380
0
0

How can I use the Dictionary object to store a global variable?
I’m trying something like:

Public dicObjects, dicFramework
set dicObjects = CreateObject("Scripting.Dictionary")
Set dicFramework = CreateObject("Scripting.Dictionary")

dicObjects ("Browser") = Browser("index:=0")
dicFramework ("DebugModeActive") = True

Marked as spam
Posted by (Questions: 188, Answers: 22)
Asked on April 29, 2014 4:28 pm
10 views
Answers (1)
2
Private answer

The dictionary object in VBScript is similar in concept to an array. Below is an example of code that you can use to store your global variables in the dictionary object and then how to read it back in.

I just used the concept of environment variables for illustration

Dim EnvVar 'This is the environment variable object alias

'Store values into the dictionary object. The first value is the 'key' and the second is the actual value:

Set EnvVar = CreateObject(''Scripting.Dictionary'')
EnvVar.Add ''Env1'', ''FirstValue''
EnvVar.Add ''Env2'', ''SecondValue''
EnvVar.Add ''Env3'', ''ThirdValue''

'You can also store variable values into the dictionary as well:
EnvVar.Add ''Env4'', fourth_val_var

'How to read the value out of the dictionary into the variable:

environment_one = EnvVar.Item(''Env1'')

MsgBox ''The value of the first environment variable is: ''& environment_one

'Clean up dictionary object once it is no longer needed. You will want to make sure that you place this in the correct place. If you destroy the object in memory too early, you won't be able to access the values stored.

Set EnvVar = Nothing

This just shows above the very basics of the dictionary object that is fine for most scripting needs. There are some advanced methods you can use, and you can look at the following reference sites as there is too much to cover in the case notes regarding this object.

Additional References:

http://msdn.microsoft.com/en-us/library/x4k5wbx4(v=vs.84).aspx

http://www.w3schools.com/asp/asp_ref_dictionary.asp

http://windowsitpro.com/scripting/understanding-vbscript-dictionary-object-alternative-arrays

Marked as spam
Posted by (Questions: 2, Answers: 438)
Answered on April 29, 2014 4:30 pm