从函数中返回多个值的方法

  There are several ways to return multiple values from functions. In this topic, we’re going to look over the 5 most common techniques to pass 2 or more values from functions. The 5 techniques are:

  1、Returning variables in Global scope

  2、Returning a Collection

  3、Returning Arrays

  4、Using Concatenated Strings

  5、Passing through the use of ByRef

  Returning variables in Global scope
  This can be achieved by declaring the variables outside the scope of the function. Here, we don’t need to pass the values through the function; but we can simply manipulate them within the function’s scope. Please note that if the same string is declared within the function, it loses its global scope – as it becomes local to the function. These variables can come from a function library or from the test script as long as they are outside the scope of the calling method. Code snippet:  

Dim intNumber_1: intNumber_1 = 40
Dim intNumber_2: intNumber_2 = 80
 
Public Sub PassValues
    intNumber_1 = intNumber_1/4
    intNumber_2 = intNumber_2/4
End Sub
 
PassValues
 
MsgBox "intNumber_1 = " & intNumber_1 &_
    vbLf & "intNumber_2 = " & intNumber_2

  Returning a Collection

  Another way to pass multiple values from a function is through the using of creating and passing Collections. We can use a collection object to store multiple values as keys/items. Code snippet:

Public Function PassValues(ByVal Num_1, ByVal Num_2)
    Set oDict = CreateObject( "Scripting.Dictionary" )
 
    With oDict
        .Add "Num_1", Num_1/4
        .Add "Num_2", Num_2/2
    End With
 
    Set PassValues = oDict
End Function
 
Set colNumbers = PassValues(40,80)
 
MsgBox "intNumber_1 = " & colNumbers.Item("Num_1") &_
    vbLf & "intNumber_2 = " & colNumbers.Item("Num_2")

  Returning Arrays

  This is quite a common technique. Each element in the array stores a variable that is then passed through the function. Code snippet:

Public Function PassValues(ByVal Num_1, ByVal Num_2)
    Dim arrArray: ReDim arrArray(2)
 
    arrArray(0) = Num_1/4
    arrArray(1) = Num_2/2
 
    PassValues = arrArray
End Function
 
arrNew = PassValues(40,80)
 
MsgBox "intNumber_1 = " & arrNew(0) &_
    vbLf & "intNumber_2 = " & arrNew(1)

  Concatenated Strings

  I have seen the usage of this technique almost as frequently as the use of arrays. Here, two or more concatenated numbers/strings can be passed through the function with the help of a delimiter. Code snippet:

Public Function PassValues(ByVal Num_1, ByVal Num_2)
    Num_1 = Num_1/4
    Num_2 = Num_2/2
 
    PassValues = Num_1 & "," & Num_2
End Function
 
sNum = PassValues(40,80)
 
MsgBox "intNumber_1 = " & Split(sNum, ",")(0) &_
    vbLf & "intNumber_2 = " & Split(sNum, ",")(1)

  Using ByRef to Pass Multiple Values

  Please refer to the article Passing Parameters ByRef and ByVal for a detailed explanation of this technique. It can be used to pass multiple values in the following manner:

Dim intNumber_1: intNumber_1 = 40
Dim intNumber_2: intNumber_2 = 80
 
Public Sub PassValues(ByRef Num_1, ByRef Num_2)
    Num_1 = Num_1/4
    Num_2 = Num_2/2
End Sub
 
PassValues intNumber_1, intNumber_2
 
MsgBox "intNumber_1 = " & intNumber_1 &_
    vbLf & "intNumber_2 = " & intNumber_2

  





posted @ 2012-02-28 10:48  Sirrah  阅读(727)  评论(0编辑  收藏  举报