ArrayList with Arrays

17,782

Short answer: The correct way to use an ArrayList Of Arrays if you just need read access (after a successful initialization):

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

output:

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 Preserve answer(UBound(answer) + 1):

No problems with an ArrayList Of ArrayLists of Arrays (as long we are talking about read access and you don't mess it up):

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)

output:

alB(0)(0)(0) => A
alB(0)(2)(2) => K
Share:
17,782
Admin
Author by

Admin

Updated on June 04, 2022

Comments

  • Admin
    Admin almost 2 years

    It seems that more "complex" ArrayLists are not widely used, since I'm unable to find any concrete, helpful info about it.

    I'm trying to create an ArrayList of Arrays (and eventually an ArrayList of ArrayLists of Arrays), but I seem unable to either add Arrays to the ArrayList, or access the Array's elements. All this is done using VBScript in QTP.

    (The code reads from an Excel file, which is working fine.)

    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
    

    Now, I realize that For Each ArrayItem in testCase is probably wrong, but I cannot find out what to use? The elements added to the ArrayList are, after all, Arrays. If I uncomment the line MsgBox "Hey!", it's written once, even though the ArrayList should have 3 Arrays.

  • Admin
    Admin over 10 years
    I'm trying to run your example in QTP (since I'm using a driver vbscript, and using vbscript in QTP. It doesn't work at all. "Option Explicit" makes the rest of the code not run. WScript.Echo isn't working.
  • Ekkehard.Horner
    Ekkehard.Horner over 10 years
    @kakemonsteret - that's why I included the line "cscript 19915175.vbs" in the output section. The script is intended as a standalone demo.
  • Admin
    Admin over 10 years
    Sorry mate - I was a bit quick and tired when looking at it. I think I've got your point now. But when I look at it, and run it, I don't see what's different from what I was doing, except the declaration of the ArrayList? (Your code is working, mind.)
  • Admin
    Admin over 10 years
    Haha - no, you found it - I just sort of understood what you were saying :-)
  • Admin
    Admin over 10 years
    What if I want to take that ArrayList (with the arrays) and insert it into ANOTHER ArrayList? That is ArrayList -> ArrayLists -> Arrays? If I try adding the ArrayList to the other ArrayList, it seems to work - it doesn't crash, at least. But if I try accessing the array elements like this: ArrayList(0)(0)(0), the code crashes.
  • Admin
    Admin over 10 years
    That is, access each individual Array in the ArrayList->ArrarList->Array. No join, since I need every separate entryin the Array.
  • Ekkehard.Horner
    Ekkehard.Horner over 10 years
    @kakemonsteret - please see update. Don't let the Join fool you, it's just for easy display.