Asp Classic将Sub放入功能......我可以吗?

时间:2015-11-05 04:59:24

标签: vbscript asp-classic

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

我可以将Sub rutine放入功能中吗?或者更好的方法呢?!

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行

     

Sub Add(s)
  ^

1 个答案:

答案 0 :(得分:3)

否 - 除了在JavaScript或称为JScript的服务器端版本中,您不能在函数中放置子函数。但是,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 提出的建议。