Search code examples
vbscripthp-uft

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




I have an issue with assigning/retrieving correct values to/from an array which is within a class in VBScript. Whenever I try to set the array's value through a stand-alone function, it just does not work and I keep getting the old array values. Here is an example code:

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

Now, when I try to set the index 2 of the given array MyClass.TestValues with the SetValue Sub, it claims the new value has been set, but when I then call GetValue for this array or print out the content of the MyClass.TestValues(2) directly, I am still getting the old values:

MyFunction.SetValue MyClass.TestValues, "newvalue2"

It returns: Value within Sub: newvalue2


But when I retrieve the value with GetValue:

 MyFunction.GetValue MyClass.TestValues

It returns: Value within Function: value2, which is the old value.


When I, however, set the array directly:

Myclass.TestValues(2) = "newvalue2"

then calling with:

MyFunction.GetValue MyClass.TestValues

gives me correct result: Value within Function: newvalue2

I am not sure whether this is a general VBScript behavior and I am making a mistake in hoping to change array values in this 'dirty' manner or whether this is strictly HP-UFT (Unified Functional Testing) related, since this is where I could observe this. I am not a profficient VBScripter either so I appreciate any help.


Solution

  • This is a documented behaviour

    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