Results 1 to 3 of 3

Thread: [RESOLVED] A Collection of Collections

Hybrid View

  1. #1

    Thread Starter
    Hyperactive Member deathfxu's Avatar
    Join Date
    Mar 2009
    Location
    USA
    Posts
    279

    Resolved [RESOLVED] A Collection of Collections

    I am starting a very data-intensive project that would benefit greatly if I could add the functionality of allowing each item in a specific Collection to be it's own Collection object.

    It would also, ideally, need to be infinitely sub-dimensionable as such. For instance (this is only for clarification, it obviously doesn't work like this):

    Code:
    Dim A As New Collection
    
    Sub Form_Load()
    Call NewSubColl(A, "B")
    Call NewSubColl(A, "C")
    Call NewSubColl(B, "D")
    End Sub
    
    Sub NewSubColl(targetColl As Collection, newSubColl)
    Dim newSubColl As New collection
    
    targetColl.Add newSubColl
    End Sub
    (Again, before anyone posts about it, i know this code will not work. I am trying to explain the desired functionality.)

    The result would be collections "B" and "C" as two separate items in collection "A", and collection "D" being an item in collection "B".

    Anyone able to accomplish this will definitely get reputation from me, and a degree of gratitude that cannot be expressed in words. Without a method of accomplishing this with collections, I will be forced to save all the data in a text file, which would make the processing time ridiculous as I will be reading and storing data constantly.

    Thank you so so so much.

  2. #2
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: A Collection of Collections

    Try this:
    Code:
    Option Explicit
    
    Dim A As Collection
    
    Sub Form_Load()
        InitColls
    End Sub
    
    Sub InitColls()
        Dim B As Collection
        Dim C As Collection
        Dim D As Collection
        
        Set A = New Collection
        Set B = New Collection
        Set C = New Collection
        Set D = New Collection
        
        A.Add Item:=B, Key:="B"
        A.Add Item:=C, Key:="C"
        B.Add Item:=D, Key:="D"
        
    End Sub
    Code:
    Sub Command1_Click()
        Dim n As Long
    
        If A Is Nothing Then InitColls
        
        Debug.Print A.Count
        Debug.Print A(1).Count
        Debug.Print A(2).Count
        Debug.Print A(1)(1).Count
        Debug.Print "---------------"
        
        n = A("C").Count + 1
        A("C").Add Item:="Item C" & n
    
        n = A("B")("D").Count + 1
        A("B")("D").Add Item:="Item D" & n
        
        Debug.Print A.Count
        Debug.Print A(1).Count
        Debug.Print A(2).Count
        Debug.Print A(1)(1).Count
        Debug.Print "---------------"
    
        n = A("B")("D").Count
        Debug.Print A("B")("D")(n)
        n = A("C").Count
        Debug.Print A("C")(n)
        Debug.Print "---------------"
    End Sub
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  3. #3

    Thread Starter
    Hyperactive Member deathfxu's Avatar
    Join Date
    Mar 2009
    Location
    USA
    Posts
    279

    Re: A Collection of Collections

    Brilliant!

    I knew there had to be functionality for it, but I could never figure out the syntax. Thanks tons!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width