1

私はこれで壁に頭をぶつけています。まず、正しい ArrayLists を ArrayList に追加できません。繰り返しの最後の ArrayList を追加し続け、前のものを上書きします。

これが本来あるべき姿です:

ArrayList testCaseList: 複数の tTestCase ArrayLists ArrayList を含む ArrayList tTestCase: 複数の tempArray 配列を含む ArrayList 配列 tempArray: 2 つの文字列エントリを含む配列 (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 を 1 回追加します。ここまでは順調ですね。次に、2 回目の繰り返しで、2 番目の tTestCase を 2 回追加し、明らかに最初の tTestCase を上書きします。次に、3 回目の繰り返しでも同じです。3 回目の tTestCase を 3 回追加し、明らかに既存のものを上書きします。

これに加えて、testCaseList 関数の OUTSIDE にアクセスしようとすると、または関数内でループの外側にある場合でも、カウントは 3 (Excel ファイルから作成された tTestCases の数) と表示されますが、それらのカウントは再び 0 です。 . したがって、そこには空の配列リストしかありません。ArrayLists は関数の外で開始されるため、これはわかりませんか?

明らかに、ここで理解できない ArrayLists への書き込みと格納に関連するものがあります。しかし、これに関する多くの関連情報を見つけることができませんでした.ArrayListはvbscriptで広く使用されていませんか? (おそらく.NETオブジェクトだから?)

4

2 に答える 2

0

コメントで述べたように、トリックは ArrayList の Clone() 関数を使用することです。これにより、単に参照を作成する代わりに、他の ArrayList に挿入できる ArrayList の (浅い) コピーが作成されます。

testCaseList.Add tTestCase.Clone()
于 2013-11-13T11:53:14.427 に答える