从函数返回多个值

时间:2014-07-07 16:24:51

标签: function vbscript asp-classic

更新:这是WIP功能。

<%
    Function ReturnTwoValues(Data)

        If Data= Now() Then
            Var1= "ABC"
            Var2= "000"
        Else
            Var1= "CDE"
            Var2= "111"
        End If

        ReturnTwoValues = Array(Var1, Var2)

    End Function

    a = ReturnTwoValues(Data)
    Value1= a(0)
    Value2= a(1)
%>

我现在的疑问是:我如何调用该功能?如果我Response.Write Value1Response.Write Value2我可以获取值,但我需要先传递参数。像ReturnTwoValues(Now())这样的东西,但如果我这样做,我怎么能得到第一个和第二个值呢?


原始问题:

我有这个伪函数,希望能够从Variable1Variable2返回值:

<%
    Today= Now()

    Function TellsMeTheTime(Date_Field)
        If IsNull(Date_Field) = False Or Date_Field <> "" Then
            DaysAmount= DateDiff("d", Date_Field, Today)
            Select Case True
                Case DaysAmount = 0
                    Variable1 = "warning"
                    Variable2 = "text1"
                Case DaysAmount > 1
                    Variable1 = "danger"
                    Variable2 = "text2 " & DaysAmount & " text3"
                Case DaysAmount = -1
                    Variable1 = "warning"
                    Variable2 = "text4"
                Case DaysAmount = -2, -3, -4, -5, -6, -7
                    Variable1 = "warning"
                    Variable2 = "text5 " & Right(DaysAmount, Len(DaysAmount) - 1) & " text6"
                Case DaysAmount <= -8
                    Variable1 = "success"
                    Variable2 = "text7 " & Right(DaysAmount, Len(DaysAmount) - 1) & " text8"
                Case DaysAmount = ""
                    Variable1 = "danger"
                    Variable2 = "text9 " & DaysAmount & " text10"
                Case Else
                    Variable1 = "warning"
                    Variable2 = "text11 " & Right(DaysAmount, Len(DaysAmount) - 1) & " text12"
            End Select
        Else
            Variable1 = "danger"
            Variable2 = "text12"
        End If
    End Function
%>

我该怎么做?

2 个答案:

答案 0 :(得分:3)

传回一个数组:

Function ReturnTwoValues(Date_Field)
    ' Do some date testing using Date_Field and then return the proper values...
    ReturnTwoValues = Array("hello", "world")
End Function

a = ReturnTwoValues(#7/7/2014#)
WScript.Echo a(0)    ' ==> "hello"
WScript.Echo a(1)    ' ==> "world"

或者利用变量在VBScript中通过引用传递的事实:

Sub ModifyTwoValues(Date_Field, returnOne, returnTwo)
    ' Do some date testing using Date_Field and then return the proper values...
    returnOne = "hello"
    returnTwo = "world"
End Sub

ModifyTwoValues #7/7/2014#, var1, var2
WScript.Echo var1    ' ==> "hello"
WScript.Echo var2    ' ==> "world"

答案 1 :(得分:0)

简单的解决方法:

  1. 使用数组或

  2. 使用字典对象。

  3. 这是一个早期的StackOverflow问题,它有一个完整的纲要:QTP: How can I return multiple Values from a Function