|
-
May 10th, 2009, 11:59 AM
#1
Thread Starter
Hyperactive Member
[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
-
May 11th, 2009, 04:37 AM
#2
Thread Starter
Hyperactive Member
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:
Dim A As New Collection
Dim B As New Collection
A.Add B, "B" 'add the B collection to the A collection
A("B").Add "computer", "digital" 'add an item to the B collection
msgbox A("B")("digital") 'this works. it msgbox's "computer"
'everything works up to this point
A("B")("digital") = "cdrom" 'this does not work
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.
-
May 11th, 2009, 05:56 AM
#3
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
-
May 11th, 2009, 06:49 AM
#4
Thread Starter
Hyperactive Member
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.
-
May 11th, 2009, 08:00 AM
#5
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.
-
May 11th, 2009, 08:13 AM
#6
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|