HP-UFT / VBscript - 通过具有数组索引引用的函数设置数组值

HP-UFT / VBscript - Setting an array value through a function with an array index reference



我有一个问题 assigning/retrieving 正确值 to/from 一个数组,它在 VBScript 中的 class 中。每当我尝试通过独立函数设置数组的值时,它就不起作用,我一直在获取旧的数组值。 这是一个示例代码:

Class NewClass

    Public TestValues

    Public Sub Init()
        TestValues = array("value0", "value1", "value2")
    End Sub
End Class

Class NewFunctions

    Public Function GetValue(xRef)
        GetValue = xRef(2)
        print "Value within Function: " & xRef(2)
    End Function

    Public Sub SetValue(xRef, xValue)
        xRef(2) = xValue
        print "Value within Sub: " & xRef(2)
    End Sub
End Class

Dim MyClass, MyFunction
Set MyClass = New NewClass
Set MyFunction = New NewFunctions

现在,当我尝试设置给定数组 MyClass.TestValues 索引 2SetValue Sub,它声称已设置新值,但是当我随后为此数组调用 GetValue 或打印出 [ 的内容时直接=56=](2),我还是得到旧值:

MyFunction.SetValue MyClass.TestValues, "newvalue2"

它returns:子内的值:newvalue2


但是当我使用 GetValue 检索值时:

 MyFunction.GetValue MyClass.TestValues

它returns:函数内的值:value2,这是旧值。


但是,当我直接设置数组时:

Myclass.TestValues(2) = "newvalue2"

然后调用:

MyFunction.GetValue MyClass.TestValues

给我正确的结果:函数内的值:newvalue2

我不确定这是否是一般的 VBScript 行为,我在希望以这种 'dirty' 方式更改数组值时犯了一个错误,或者这是否与 HP-UFT(统一功能测试)严格相关,因为这是哪里我可以观察到这一点。 我也不是一个熟练的 VBScripter,所以我很感激任何帮助。

这是documented行为

Argument in a Class

If the parameter is specified as ByRef, the argument is passed by value if the variable sent as an argument is in a class