Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

access dictionary outside function

Status
Not open for further replies.

ralphtrent

Programmer
Jun 2, 2003
958
0
0
US
Can I access my dictionary object outside this function?
Function dict(file,state)
Dim dict1
z=0
set dict1 = CreateObject("scripting.dictionary")
call dict1.add(file,state)
For Each key In dict1
For Each item In dict1.keys
If dict1(key) = False Then WScript.Echo(key & " does NOT exsist and the Script can not continue"):z=1
If dict1(key) = True Then WScript.Echo(key & " does exsist")
next
Next
If z=1 Then quit()
' If z = 0 Then install()
End Function
'I want to do something like this
wscript.echo dict.count
'but I get object required error.
 
ralphtrent,
There are at least three ways to do this. One is to declare your dictionary object as a global. I tried this and it works:

Option Explicit

Dim dict1
set dict1 = CreateObject("scripting.dictionary")

dict "file", 1

Function dict(file,state)
Dim z, key, item
z=0
call dict1.add(file,state)
For Each key In dict1
For Each item In dict1.keys
If dict1(key) = False Then WScript.Echo(key & " does NOT exsist and the Script can not continue"):z=1
If dict1(key) = True Then WScript.Echo(key & " does exsist")
next
Next
If z=1 Then quit()
' If z = 0 Then install()
End Function

WScript.Echo dict1.count

Another is to pass it out as a return parameter. That is somewhere in your function set the function name equal to the dictionary object: dict = dict1. I did not try this.

A third way is to pass the object by reference. This is simialr to a pointer in c, and also allows you to have more than one value "returned" from your function. Change your function declaration to dict(ByVal file, ByVal state, ByRef objDict). I did not ry this either.

Zy
 
ralphtrent,
Here is option 2 tested:

Code:
Option Explicit
Dim objReturn

Set objReturn = dict("file", 1)

Function dict(file,state)
    Dim dict1
    Dim z, key, item
    z=0
    set dict1 = CreateObject("scripting.dictionary")
    call dict1.add(file,state)
    For Each key In dict1
        For Each item In dict1.keys
            If dict1(key) = False Then WScript.Echo(key & " does NOT exsist and the Script can not continue"):z=1
            If dict1(key) = True Then WScript.Echo(key & " does exsist")
        next
    Next
    Set dict = dict1
    If z=1 Then quit()
'    If z = 0 Then install()
End Function

WScript.Echo objReturn.count

Standby for option 3...

Zy
 
Voila!

Code:
Option Explicit
Dim objReturn

Script.Clear

dict "file", 1, objReturn

Function dict(ByVal file, ByVal state, ByRef objParam)
	Dim dict1
	Dim z, key, item
    z=0
	set dict1 = CreateObject("scripting.dictionary")
    call dict1.add(file,state)
    For Each key In dict1
        For Each item In dict1.keys
            If dict1(key) = False Then WScript.Echo(key & " does NOT exsist and the Script can not continue"):z=1
            If dict1(key) = True Then WScript.Echo(key & " does exsist")
        next
    Next
    Set objParam = dict1
    If z=1 Then quit()
'    If z = 0 Then install()
End Function

WScript.Echo objReturn.count

Zy
 
Zy
thanks for your replies. I will test them out.

Ralph
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top