Khrys
Khrys

Reputation: 2774

Return more than one value from a function

UPDATE: This is the WIP Function.

<%
    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)
%>

My doubt now is: How can I invoke the function? If I do Response.Write Value1 and Response.Write Value2 I can get the value, but I need to pass the parameter first. Something like ReturnTwoValues(Now()), but if I do it, how could I be able to get the first and the second value?


ORGINAL QUESTION:

I have this pseudo function and would like to be able to return the value from Variable1 and Variable2:

<%
    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
%>

How can I do this?

Upvotes: 3

Views: 4781

Answers (2)

Bret
Bret

Reputation: 376

Easy workarounds:

  1. Use an array, or

  2. Use a dictionary object.

Here's an earlier StackOverflow question that has a full rundown: QTP: How can I return multiple Values from a Function

Upvotes: 0

Bond
Bond

Reputation: 16321

Pass back an array:

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"

Or take advantage of the fact that variables are passed by reference in 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"

Upvotes: 5

Related Questions