|
-
Aug 20th, 2006, 09:51 AM
#1
Thread Starter
Junior Member
[RESOLVED] Displaying the "Key" in a Collection
Using VB6 Pro, Win XP Pro. I have no trouble displaying the "Item" in a Collection using either the for each next loop or just a straight for loop that uses the index of the Collection. Is there a way to display the "Key" in a Collection? Thanks in advance for any help on this. JungleJim.
-
Aug 20th, 2006, 10:14 AM
#2
Re: Displaying the "Key" in a Collection
Isn't there Item.Key property ?..
-
Aug 21st, 2006, 10:10 AM
#3
Thread Starter
Junior Member
Re: Displaying the "Key" in a Collection
Thank you for your response. There may be such a property RhinoBull, but I have been unable to find it in Microsft Visual Basic 6.0 Language Reference and it doesn't work when I try to use it. JungleJim
-
Aug 21st, 2006, 10:25 AM
#4
Re: Displaying the "Key" in a Collection
While collections of course have keys, there is no Key property and so as far as I know you can't display the key value, only the item value.
-
Aug 21st, 2006, 10:52 AM
#5
Re: Displaying the "Key" in a Collection
There is no KEY property, but it's easy to add one.
You can add the KEY to the actual item
This is what I've done, and it works, but the only problem is that it stores the KEY 2 times, one as the actual KEY, and another in the Value.
Put the following code in a Class module, and name it "CollectionKEY"
VB Code:
Option Explicit
'local variable to hold collection
Private mCol As Collection
Public Sub Add(Item As Variant, Optional sKey As String, Optional Before As Variant, Optional After As Variant)
'create a new object
Dim objNewMember() As Variant
'set the properties passed into the method
ReDim objNewMember(0 To 1)
If IsObject(Item) Then
Set objNewMember(0) = Item
Else
objNewMember(0) = Item
End If
objNewMember(1) = sKey
If Len(sKey) = 0 Then
mCol.Add objNewMember, , Before, After
Else
mCol.Add objNewMember, sKey, Before, After
End If
End Sub
Public Property Get Item(vntIndexKey As Variant) As Variant
Dim vItem() As Variant
vItem = mCol(vntIndexKey)
If IsObject(vItem(0)) Then
Set Item = vItem(0)
Else
Item = vItem(0)
End If
End Property
Public Property Get ItemKEY(vntIndexKey As Variant) As String
ItemKEY = mCol(vntIndexKey)(1)
End Property
Public Property Get Count() As Long
'used when retrieving the number of elements in the
'collection. Syntax: Debug.Print x.Count
Count = mCol.Count
End Property
Public Sub Remove(vntIndexKey As Variant)
'used when removing an element from the collection
'vntIndexKey contains either the Index or Key, which is why
'it is declared as a Variant
'Syntax: x.Remove(xyz)
mCol.Remove vntIndexKey
End Sub
Public Property Get NewEnum() As IUnknown
'this property allows you to enumerate
'this collection with the For...Each syntax
Set NewEnum = mCol.[_NewEnum]
End Property
Private Sub Class_Initialize()
'creates the collection when this class is created
Set mCol = New Collection
End Sub
Private Sub Class_Terminate()
'destroys collection when this class is terminated
Set mCol = Nothing
End Sub
This is how you use it:
VB Code:
Option Explicit
Private Sub Form_Load()
Dim ClsKey As New CollectionKEY, Itm As Variant
Dim K As Long
ClsKey.Add "testing... 1", "KEY1"
ClsKey.Add "testing... 2", "KEY2"
ClsKey.Add "testing... 3", "KEY3"
ClsKey.Add "testing... 4", "KEY4", "KEY1"
ClsKey.Add "testing... 5", "KEY5", , "KEY2"
For K = 1 To ClsKey.Count
Debug.Print ClsKey.Item(K), ClsKey.ItemKEY(K)
Next K
Debug.Print
' using For Each
For Each Itm In ClsKey
Debug.Print Itm(0), Itm(1)
Next Itm
End Sub
-
Aug 21st, 2006, 10:56 AM
#6
Re: Displaying the "Key" in a Collection
Just out of curiosity Jim, why do you need the key to be different than the item value?
-
Aug 21st, 2006, 11:04 AM
#7
Re: Displaying the "Key" in a Collection
 Originally Posted by MartinLiss
Just out of curiosity Jim, why do you need the key to be different than the item value?
Isn't the whole point of the KEY for faster access of the value, where the KEY is something like an ID ?
For example when you add records from a table to collection, you would do something like:
VB Code:
MyCollection.Add Recordset.Fields("Value").Value, "KEY_" & Recordset.Fields("ID").Value
And then you use the collection for faster random access by ID...
That's what I use it for (among other things)...
-
Aug 21st, 2006, 11:29 AM
#8
Re: Displaying the "Key" in a Collection
 Originally Posted by JungleJim
Thank you for your response. There may be such a property RhinoBull, but I have been unable to find it in Microsft Visual Basic 6.0 Language Reference and it doesn't work when I try to use it. JungleJim
Sorry for the confusion, Jim. I forgot that item's key is not exposed through "normal" interface...
-
Aug 21st, 2006, 01:07 PM
#9
Re: Displaying the "Key" in a Collection
I faced a similar problem in the past and this is how it was resolved:
[Resolved] How To Retrieve Keys from Collection?
Pradeep
-
Aug 21st, 2006, 01:11 PM
#10
Re: Displaying the "Key" in a Collection
I you have not started coding yet or it is possible for you to modify your program, I suggest you use a Dictionary object insted of Collection which exposes both Key and value. Otherwise the link in my previous post may be of help to you.
Pradeep
-
Aug 21st, 2006, 01:14 PM
#11
Re: Displaying the "Key" in a Collection
 Originally Posted by Pradeep1210
That's the same as my example, using an array...
Except the first item in your array is never used 
Dim sCollectionData(2)
is the same as sCollectionData(0 to 2) which is 3 items
-
Aug 21st, 2006, 01:22 PM
#12
Re: Displaying the "Key" in a Collection
Good catch 
Actually I have a have a habit ou using Option Base 1 so that all my arrays start with 1.
Thanks for pointing this out though 
Pradeep
-
Aug 21st, 2006, 01:41 PM
#13
Re: Displaying the "Key" in a Collection
 Originally Posted by Pradeep1210
...Actually I have a have a habit ou using Option Base 1 so that all my arrays start with 1...
That may give a major headach to those who will come after you to support your piece of art.
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
|