Asp classic VBScript函数能否返回字典?

Asp classic VBScript函数能否返回字典?,asp-classic,dictionary,vbscript,Asp Classic,Dictionary,Vbscript,我有一个表单数据字典,我想使用函数修改它 function queryCleanForm(myDictForm) dim arrayKeys arrayKeys = myDictForm.keys for i=0 to myDictForm.count-1 myDictForm(arrayKeys(i)) = replace(myDictForm(arrayKeys(i)), "'", "''") response.write myDi

我有一个表单数据字典,我想使用函数修改它

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
问题是行
queryCleanForm=myDictForm
错误如下

Wrong number of arguments or invalid property assignment 

有没有在VBScript中执行此操作的方法?

是的,您需要使用SET命令:

设置queryCleanForm=myDictForm

尝试以下操作:

SET queryCleanForm = myDictForm

对于对象,您需要使用SET告诉VBScript这是一个对象引用,而不是赋值类型。

您也可以在函数中使用ByRef或ByVal值。ByVal,发送给函数或子函数的对象将复制到私有memmory中,在函数内部使用,并在函数完成后丢弃。ByRef,您发送给函数的对象被引用,并且您所做的所有操作(移除关键点、设置对象等)都直接对您发送的对象执行

例如

子测试
DIM testDict作为变体
调用setdict(testDict)
testDict.添加“测试”、“值”
调用addValue(testDict,“test2”,“另一个值”)
msgbox testDict.Count
Set testDict=Nothing
端接头
Sub-setdict(ByRef作为变体)
如果Typename(在字典中)是“字典”,那么
在_Dict=CreateObject(“Scripting.Dictionary”)中设置
如果结束
端接头
子addValue(ByRef in_Obj作为变量,ByVal in_键作为字符串,ByVal in_值作为字符串)
如果不在对象中存在(在键中),则
in_Obj.Add in_键,in_值
如果结束
端接头
test sub使用variant类型的变量调用sub setdict。在函数中,我验证发送给sub的对象的类型。如果对象类型不是dictionary对象(事实并非如此),则_Dict中的对象(实际上是sub test中声明的testDict对象)将设置为dictionary对象


为了更好地演示引用,我还包括了第二个子项addvalue。我再次将对象作为引用传递给函数,并向dictionary对象添加另一个键。在主测试子模块中,然后发布计数。在这种情况下,有2把钥匙。

这个问题与你自己的问题5分钟前问的问题非常相似。哇!(符合最低字符要求)
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