Vbscript Asp经典把子函数放在函数中。。。我可以吗?

Vbscript Asp经典把子函数放在函数中。。。我可以吗?,vbscript,asp-classic,Vbscript,Asp Classic,为什么现在返回…语法错误 我可以在函数中加入次芸香碱吗?还是更好的办法 Function SumerizePlanArrays(f_String, f_Type) Set dic = CreateObject("Scripting.Dictionary") Sub Add(s) weight = Split(s,"$")(0) values = Split(s,"$")(1) pipes = Split(values, "|")

为什么现在返回…语法错误

我可以在函数中加入次芸香碱吗?还是更好的办法

Function SumerizePlanArrays(f_String, f_Type)

   Set dic = CreateObject("Scripting.Dictionary")
   Sub Add(s)
       weight = Split(s,"$")(0)
       values = Split(s,"$")(1)
       pipes = Split(values, "|")
       For Each line In pipes
           val = Split(line, ",")
           if f_Type = 1 then
               dic(val(1)) = (dic(val(1))*weight/100) + CInt(val(2))
           elseif f_Type = 2 then
               dic(val(1)) = dic(val(1)) + CInt(val(2))
           end if
       Next
   End Sub

   arrString = Split(f_String,"#")
   For i = 0 to UBound(arrString)
       'wei = Split(arrString(i),"$")(0)
       Add arrString(i)
   Next

   Set a = CreateObject("System.Collections.ArrayList")
   For Each key In dic.Keys
       a.Add "0," & key & "," & dic(key)
   Next
   a.Sort
   result = Join(a.ToArray, "|")

   SumerizePlanArrays = result

End Function
Microsoft VBScript编译错误“800a03ea”

语法错误

/inc_func_projects.asp,第2592行

子添加项
^


否-除了JavaScript或名为JScript的服务器端版本外,不能在函数中放置sub。然而,VBScript和JScript是两种完全不同的语言

你应该这样做

Function SumerizePlanArrays(f_String, f_Type)

   Set dic = CreateObject("Scripting.Dictionary")

   arrString = Split(f_String,"#")
   For i = 0 to UBound(arrString)
       'NOTE: Updated the call to reflect comment by sadrasjd...
       Add arrString(i, f_Type, dic)
   Next

   Set a = CreateObject("System.Collections.ArrayList")
   For Each key In dic.Keys
       a.Add "0," & key & "," & dic(key)
   Next
   a.Sort
   result = Join(a.ToArray, "|")

   SumerizePlanArrays = result

End Function

Sub Add(s, type, dic)
    'NOTE: ^Updated the parameters to reflect comment by sadrasjd^
    weight = Split(s,"$")(0)
    values = Split(s,"$")(1)
    pipes = Split(values, "|")
    For Each line In pipes
        val = Split(line, ",")
        if type = 1 then
            dic(val(1)) = (dic(val(1))*weight/100) + CInt(val(2))
        elseif type = 2 then
            dic(val(1)) = dic(val(1)) + CInt(val(2))
        end if
    Next
End Sub

注意:更新了呼叫以反映sadrasjd提出的建议。

Errr。。。为什么?函数用于返回值,而子函数不返回值,但两者都是具有特定用途的不同代码块。您也可以使用类,然后它可以有自己的方法。您可以将函数或子函数“放入”到另一个函数或子函数中,使用代码e在父函数内动态创建它。g<代码>执行“子消息(x):MsgBox x:End Sub”,此子消息不显示在全局范围内,只能在父节点内调用,并且它一直存在到父节点结束。但是这个技巧不会给你机会,比如JavaScript中的闭包,因为嵌套子没有访问父对象的范围,它只能在全局范围内工作。@ SADRASJD如果解决了这个问题,就考虑接受它。看,这正是你发布的问题的解决方案。如果你想让我看看代码背后的逻辑,我可以。我会做一些修改。@Paul是的,就像在初学者中使用一个普通的
Array()
<代码>设置a=CreateObject(“System.Collections.ArrayList”)真恶心@兰克马特:呵呵!你必须从某个地方开始。。。我有一点担心,这篇文章的前两行。另外,
选项Explicit
没有设置。@sadrasjd:你真的应该打开;这会为你以后省去很多痛苦。