Can a VBScript function return a dictionary?

I have a dictionary of form data that I want to change using a function.

function queryCleanForm(myDictForm) dim arrayKeys arrayKeys = myDictForm.keys for i=0 to myDictForm.count-1 myDictForm(arrayKeys(i)) = replace(myDictForm(arrayKeys(i)), "'", "''") response.write myDictForm(arrayKeys(i)) next queryCleanForm = myDictForm end function 

The problem is the queryCleanForm = myDictForm line queryCleanForm = myDictForm like

 Wrong number of arguments or invalid property assignment 

Is there a way to do this in VBScript?

+6
dictionary vbscript asp-classic
source share
3 answers

Try the following:

 SET queryCleanForm = myDictForm 

With objects, you need to use SET to tell VBScript that it is a reference to the object that you are assigning is not a value type.

+14
source share

Yes, you need to use the SET command:

Set queryCleanForm = myDictForm

+1
source share

You can also use ByRef or ByVal values ​​in a function. ByVal, the object you sent the function or sub to is copied to a private memmory for use inside the function and discarded after the function finishes. ByRef, the object to which you sent the function refers to all the manipulations you made, deleting keys, setting the object, etc. It is directly done for the object you sent.

eg.

 Sub test DIM testDict as variant call setdict(testDict) testDict.Add "test", "value" call addValue(testDict, "test2","another value") msgbox testDict.Count Set testDict = Nothing End Sub Sub setdict(ByRef in_Dict as Variant) If Typename(in_Dict) <> "Dictionary" Then SET in_Dict = CreateObject("Scripting.Dictionary") end if end sub sub addValue(ByRef in_Obj as Variant, ByVal in_Key as String, ByVal in_Value as String) if not in_Obj.Exists(in_Key) then in_Obj.Add in_Key, in_Value end if end sub 

Test subheadings with a variable type variant to a subset. In the function, I check the type of object sent to sub. If the type of the object is not a dictionary object (which it is not), then the in_Dict object, which is actually the testDict object declared in the test, will be installed on the dictionary object.

To demonstrate the link better, I also included the second addvalue subtask. I pass the object again to the function as a reference and add another key to the dictionary object. In the main test sub-selection, then submit the score. In this case, there are 2 keys.

0
source share

All Articles