Results 1 to 6 of 6

Thread: [RESOLVED] A Collection of Collections - Setting an item

  1. #1

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

    Resolved [RESOLVED] A Collection of Collections - Setting an item

    This post is worded poorly. Please read post #2.


    I was given this code in a previous resolved thread:

    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
    
    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, Key:="ninja"
        
        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 "---------------"
    
        A("B")("D").Item("ninja") = "woot woot!"
        Debug.Print A("B")("D")("ninja")
    
    End Sub
    I am using the wrong syntax on the bold line. Does anyone know how I am supposed to do that if I do not know the index number, and only know the key ("ninja")?
    Last edited by deathfxu; May 11th, 2009 at 04:38 AM. Reason: clarification

  2. #2

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

    Re: A Collection of Collections - Setting an item

    I think I asked this in a more intimidating way than intended. I don't care about the code above.

    I have a Collection of Collection objects. I just want to know how you change one of the items in it. Below is some sample code (just for your understanding).

    vb Code:
    1. Dim A As New Collection
    2. Dim B As New Collection
    3.  
    4. A.Add B, "B" 'add the B collection to the A collection
    5. A("B").Add "computer", "digital" 'add an item to the B collection
    6. msgbox A("B")("digital") 'this works. it msgbox's "computer"
    7. 'everything works up to this point
    8.  
    9. A("B")("digital") = "cdrom" 'this does not work
    10. A("B").Item("digital") = "dvdrom" 'this also does not work

    Does anyone know how I can format this without using the index number?
    Last edited by deathfxu; May 11th, 2009 at 04:42 AM.

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

    Re: A Collection of Collections - Setting an item

    I don't think you can simply change the content of an item of a collection like that.
    Try this:
    Code:
    Option Explicit
    
    Dim A As New Collection
    Dim B As New Collection
    
    Sub Form_Load()
        A.Add B, "B"                'add the B collection to the A collection
        '-- instead off: A("B").Add "computer", "digital"
        B.Add "computer", "digital" 'add an item to the B collection
        
        '-- both of these lines give you the samething: "computer"
        MsgBox A("B")("digital")
        MsgBox B("digital")
        
        '-- modify existing item "digital"
        AddOrModifyItem B, "cdrom", "digital"
        MsgBox B("digital") '-- "cdrom"
        '-- add new item "analog"
        AddOrModifyItem B, "LCD", "analog"
        MsgBox B("analog") '-- "LCD"
    End Sub
    
    Sub AddOrModifyItem(ByRef X As Collection, ByVal Item As Variant, ByVal Key As String)
        On Error Resume Next
        X.Remove Key '-- remove item if already existed
        On Error GoTo 0
        X.Add Item, Key
    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

  4. #4

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

    Re: A Collection of Collections - Setting an item

    I wasn't sure if it was possible and I wasn't formatting it correctly, or what the deal was.

    How about this... Any way of determining the Key of a Collection Item knowing only the Index number? Being able to also determine the Index number knowing only the Key would also be very beneficial.

    Btw, already rep'd ya.

  5. #5
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: A Collection of Collections - Setting an item

    Unfortunately, VB collections do not expose that relationship. There are hacks that can be used to get a key from an index and vice versa. Here is one on PSC. Another option is to create/use custom collection classes, many of these are about on PSC and other sites also.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  6. #6

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

    Re: A Collection of Collections - Setting an item

    That is precisely what I needed. Thank you very much LaVolpe.

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