Reputation: 471
I am doing some ASP Classic with VBScript. I am doing something like the following,
Public Sub testFunc(ByRef a)
a=10
End Sub
Public Function createDict()
Set createDict = CreateObject("Scripting.Dictionary")
createDict.Add "foo",0
End Function
Set b = createDict()
testFunc b("foo")
Response.Write(b.Item("foo")) 'The result is 0, instead of 10
I am wondering how to create a function/sub that can modify the dictionary item. Is it possible to do so in VBScript?
Even I tried with
Set c = CreateObject("Scripting.Dictionary")
c.Add "foo",0
testFunc c("foo")
Response.Write(c("foo"))
It doesn't work either.
Upvotes: 2
Views: 1703
Reputation: 1118
As far as VBScript goes the below works just fine for me...
Sub Test()
Dim X, W
Set X = Nothing
Set X = PassDictionary
If Not X Is Nothing Then
For Each W In X.Keys
MsgBox W & " : " & X.Item(W)
Next
End If
End Sub
Function PassDictionary()
Dim objDic
Set objDic = CreateObject("Scripting.Dictionary")
objDic.Add "One", objDic.Count + 1
objDic.Add "Two", objDic.Count + 1
objDic.Add "Three", objDic.Count + 1
Set PassDictionary = objDic
End Function
EDIT: By adding an additional Argument and passing the Dictionary object itself to the "testfunc" routine I was able to modify the Dictionary Object.
Public Sub testFunc(a, b)
a(b) = 10
End Sub
Public Function createDict()
Set createDict = CreateObject("Scripting.Dictionary")
createDict.Add "foo", 0
End Function
Set b = createDict()
Call testFunc(b, "foo")
Response.Write (b.Item("foo"))
Upvotes: 1
Reputation: 38755
If you use an expression like collection(index)
in other contexts than left of an assignment (=), you get a (copy of the) value of the collection's element at that index. So a Sub or Function to change an element must get the collection, the index, and the new value via its parameter list.
Upvotes: 2