Qtp和vbscript:Arraylists的ArrayList,带有一个函数内部(和外部)的数组

时间:2013-11-12 22:14:37

标签: vbscript arraylist qtp

我正撞在墙上撞到墙上。首先,我无法将正确的ArrayLists添加到ArrayList;它不断在迭代中添加最后一个ArrayList并覆盖前一个。

这应该是这样的:

ArrayList testCaseList:包含多个tTestCase ArrayLists的ArrayList ArrayList tTestCase:包含多个tempArray数组的ArrayList Array tempArray:包含两个字符串条目的数组(从Excel文件中读取)

以下是相关代码:

'The ArrayList with ArrayLists (test cases):
Dim testCaseList : Set testCaseList = CreateObject("System.Collections.ArrayList")
'Temporary ArrayList containing test cases:
Dim tTestCase : Set tTestCase = CreateObject("System.Collections.Arraylist")

Set my_sheet = ExcelObject.sheets.item(testCaseSheet)

'Function that reads the test cases from the Excel file:
Function getTestsCaseActions (row, col)
    Do While my_sheet.cells(row, 2).Value <> ""
        'The first array to add to tTestCase:
        tempArray = array(my_sheet.cells(row, 2), my_sheet.cells(row, 3))
        tTestCase.Add tempArray

        'Go through the rows and columns and get the rest of the arrays to add:
        Do While my_sheet.cells(row, col).Value <> ""
            tTestCase.Add array(my_sheet.cells(row, col), my_sheet.cells(row+1, col))
            col = col+1
        Loop
        'We now have a tTestCase ArrayList complete with the test case arrays.

        'Test print the arrays in the tTestCase Arraylist:
        'Dim i
        'For i=0 To tTestCase.Count-1
        '    MsgBox tTestCase(i)(0) & " -> " & tTestCase(i)(1)  'Works fine.
        'Next

        'Add the tTestCase ArrayList to the testCastList ArrayList:
        testCaseList.Add tTestCase

        'Test:
        MsgBox testCaseList.count     'This LOOKS right - the count increases for each iteration
        Dim y
        For y=0 To testCaseList.Count-1
            MsgBox "Added to testCaseList: " & testCaseList(y)(0)(0)
        Next
        'But no. This is how the printout looks for each iteration:
        'Iteration 0: TC01
        'Iteration 1: TC02
        '             TC02
        'Iteration 2: TC03
                      TC03
        '             TC03

        tTestCase.Clear

        row = row+2
        col = 4
    Loop
End Function

getTestsCaseActions 3, 4

'MsgBox testCaseList.Count        'This shows 3, which appears correct...
'MsgBoc testCaseList(0).Count     'But this shows zero...? Should be 5...

正如我在代码注释中提到的,测试每次迭代的tTestCase ArrayList表明数据被正确读取并存储到其中。

但是当我将tTestCase ArrayList添加到testCaseList ArrayList时,它不起作用。在第一次迭代中,它添加第一个tTestCase一次。到现在为止还挺好。然后在第二次迭代中,它将第二个tTestCase添加两次,显然会覆盖第一个。然后它在第三次迭代中是相同的:它将第三个tTestCase添加三次,显然会覆盖现有的。

除此之外,如果我尝试访问函数的testCaseList OUTSIDE - 或者甚至在函数中但在循环外部,计数显示3(这是从Excel文件创建的tTestCases的数量),但它们的数量再次是0.所以,除了空的arraylists之外什么也没有。这个我不明白,因为ArrayLists是在函数之外启动的吗?

显然,在ArrayLists中存在与编写和存储相关的内容,我在这里并不理解。但我一直无法找到关于此的相关信息 - 在vbscript中没有广泛使用的ArrayList? (也许因为它是一个.NET对象?)

2 个答案:

答案 0 :(得分:1)

解决ArrayLists问题的一个可靠方法是忘记ArrayLists是指定为引用的对象。将alA放入alB然后修改/清除alA将在alB中显示,因为alB包含对alA的引用(不是副本)。演示代码:

Option Explicit

Dim alA : Set alA = CreateObject("System.Collections.ArrayList")
alA.Add "one"
alA.Add "two"
Dim alB : Set alB = CreateObject("System.Collections.ArrayList")
alB.Add alA
alB.Add alA

Dim e
For Each e In alB
    WScript.Echo 0, e(0), e(1)
Next

WScript.Echo "----------"

WScript.Echo "alA(0)    = ""eins"""
alA(0)    = "eins"
WScript.Echo "alB(1)(1) = ""zwei"""
alB(1)(1) = "zwei"

For Each e In alB
    WScript.Echo 1, e(0), e(1)
Next
WScript.Echo 2, "alA(1)", alA(1)

WScript.Echo "----------"

alA.Clear
WScript.Echo "After alA.Clear"

For Each e In alB
    WScript.Echo 3, e.Count
Next

WScript.Echo "----------"

WScript.Echo "alA Is alB(0)", CStr(alA Is alB(0))
WScript.Echo "alA Is alB(1)", CStr(alA Is alB(1))

输出:

cscript 2-19941052.vbs
0 one two
0 one two
----------
alA(0)    = "eins"
alB(1)(1) = "zwei"
1 eins zwei
1 eins zwei
2 alA(1) zwei
----------
After alA.Clear
3 0
3 0
----------
alA Is alB(0) True
alA Is alB(1) True

更新评论:

这很容易检查,使用

alB.Add alA.Clone()
alB.Add alA.Clone()

For Each e In alB
    WScript.Echo 3, e.Count, e(0), e(1)
Next

你会得到:

cscript 3-19941052.vbs
0 one two
0 one two
----------
alA(0)    = "eins"
alB(1)(1) = "zwei"
1 one two
1 one zwei
2 alA(1) two
----------
After alA.Clear
3 2 one two
3 2 one zwei
----------
alA Is alB(0) False
alA Is alB(1) False

您自己已经提到了警告:如果您的Sub-ArrayLists包含引用/对象,那么仍然存在龙。

答案 1 :(得分:0)

如评论中所述,诀窍是使用ArrayList的Clone()函数。这将创建ArrayList的(浅)副本,该副本可以插入到另一个ArrayList中,而不是仅创建引用。

testCaseList.Add tTestCase.Clone()