3

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?

Khrys
  • 2,670
  • 9
  • 49
  • 82
  • It's simple, it's returning an Array so call the element you want to `Response.Write()` like `Response.Write(ReturnTwoValues(Now())(0))` will return the first element from the Array. – user692942 Apr 20 '17 at 20:38

2 Answers2

5

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"
Bond
  • 16,071
  • 6
  • 30
  • 53
  • Using your first example, how could I pass a parameter? – Khrys Jul 07 '14 at 18:34
  • Just pass it in the usual way. `Function ReturnTwoValues(Date_Field)`. And then call it with `a = ReturnTwoValues(#7/7/2014#)` or any other date value. – Bond Jul 07 '14 at 18:36
  • Ok, but how will I response.write with the parameter? Usually I would do `Response.Write ReturnTwoValues(someParam)`. But as I have 2 values to return, how I will be able to get it? – Khrys Jul 07 '14 at 18:38
  • Save the return value into a variable first. Like the way I used variable `a` in my example. That variable will actually be an array holding two values. `a(0)` will be the first value and `a(1)` will be the second value. To write them, just use `Response.Write a(0)` or `Response.Write a(1)`. I've also updated the code examples to show passing your `Date_Field` parameter. – Bond Jul 07 '14 at 18:40
  • Thanks! I was missing it. – Khrys Jul 07 '14 at 18:44
  • I still missing something. Can you light me up? I have updated the question with your help. Thanks. – Khrys Jul 07 '14 at 19:00
  • Your updated code looks fine. Just make sure `Data` is the date value you want to test. – Bond Jul 07 '14 at 19:44
  • Yeah, the function code is ok. What I am still missing is how I will call the function to get the 2 values. In the code example I used `Now()`, but I will have N dates possibilities. – Khrys Jul 07 '14 at 20:01
  • You lost me. How did you plan on calling your `TellMeTheTime` function originally? It should be no different. Call the function, passing any date value to it. And return an `Array()` from that function that contains the two variable values, like in my first example. – Bond Jul 07 '14 at 20:16
  • This is the question. I dunno how to call the function passing a parameter and return value1 when I want and value2 when I want. I will do more tests. Thanks. – Khrys Jul 08 '14 at 12:03
  • Well, like in my first example, the statement `a = ReturnTwoValues(#7/7/2014#)` is how you call the function, passing the date `7/7/2014` as the parameter. The function will always return two values (I thought that was the point?) into an array named `a`. Your two return values will be `a(0)` and `a(1)`. At that point, you can use one, both, or neither. Up to you. – Bond Jul 08 '14 at 12:42
0

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

Community
  • 1
  • 1
Bret
  • 274
  • 2
  • 10