似乎更多“复杂”的ArrayLists没有被广泛使用,因为我无法找到任何有关它的具体,有用的信息。
我正在尝试创建一个数组的ArrayList(最终是一个数组ArrayList的ArrayList),但我似乎无法将数组添加到ArrayList,或者访问数组的元素。所有这些都是在QTP中使用VBScript完成的。
(代码从Excel文件中读取,工作正常。)
Set my_sheet = ExcelObject.sheets.item(testCaseSheet)
testCase = CreateObject("System.Collections.ArrayList")
Function getTestsCaseActions (row, col)
Do While my_sheet.cells(row, 2).Value <> ""
MsgBox tempArray(0) & " -> " & tempArray(1) 'WORKS FINE - THE VALUES ARE PRINTED
testCase.Add tempArray
row = row+2
Loop
End Function
getTestsCaseActions 3, 4
'This is not working - how do I access the arrays and their values in the arraylist?
For Each ArrayItem in testCase
MsgBox ArrayItem(0)' & ", " & ArrayItem(1)
'MsgBox "Hey!"
Next
现在,我意识到For Each ArrayItem in testCase
可能是错误的,但我找不到要使用的内容?毕竟,添加到ArrayList的元素是Arrays。如果我取消注释行MsgBox "Hey!"
,它会被写入一次,即使ArrayList应该有3个数组。
答案 0 :(得分:1)
简答:如果您只需要读取权限(成功初始化后),使用ArrayList Of Arrays的正确方法:
Option Explicit
Dim alA : Set alA = CreateObject("System.Collections.Arraylist")
alA.add Split("A B C")
alA.add Split("D E F")
alA.add Split("I J K")
WScript.Echo "---- For Each In"
Dim aX
For Each aX In alA
WScript.Echo TypeName(aX), Join(aX)
Next
WScript.Echo "---- For To"
Dim i
For i = 0 To alA.Count - 1
WScript.Echo TypeName(alA(i)), Join(alA(i))
Next
输出:
cscript 19915175.vbs
---- For Each In
Variant() A B C
Variant() D E F
Variant() I J K
---- For To
Variant() A B C
Variant() D E F
Variant() I J K
ReDim保留答案(UBound(答案)+ 1):
ArrayList的ArrayList没有问题(只要我们谈论读取访问而你就不会搞砸了):
Dim alB : Set alB = CreateObject("System.Collections.Arraylist")
alB.Add alA
WScript.Echo "alB(0)(0)(0) =>", alB(0)(0)(0)
WScript.Echo "alB(0)(2)(2) =>", alB(0)(2)(2)
输出:
alB(0)(0)(0) => A
alB(0)(2)(2) => K