Results 1 to 13 of 13

Thread: [RESOLVED] Displaying the "Key" in a Collection

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2006
    Posts
    29

    Resolved [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.

  2. #2

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jan 2006
    Posts
    29

    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

  4. #4

  5. #5
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    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:
    1. Option Explicit
    2.  
    3. 'local variable to hold collection
    4. Private mCol As Collection
    5.  
    6. Public Sub Add(Item As Variant, Optional sKey As String, Optional Before As Variant, Optional After As Variant)
    7.     'create a new object
    8.     Dim objNewMember() As Variant
    9.    
    10.     'set the properties passed into the method
    11.     ReDim objNewMember(0 To 1)
    12.    
    13.     If IsObject(Item) Then
    14.         Set objNewMember(0) = Item
    15.     Else
    16.         objNewMember(0) = Item
    17.     End If
    18.    
    19.     objNewMember(1) = sKey
    20.    
    21.     If Len(sKey) = 0 Then
    22.         mCol.Add objNewMember, , Before, After
    23.     Else
    24.         mCol.Add objNewMember, sKey, Before, After
    25.     End If
    26. End Sub
    27.  
    28. Public Property Get Item(vntIndexKey As Variant) As Variant
    29.     Dim vItem() As Variant
    30.     vItem = mCol(vntIndexKey)
    31.    
    32.     If IsObject(vItem(0)) Then
    33.         Set Item = vItem(0)
    34.     Else
    35.         Item = vItem(0)
    36.     End If
    37. End Property
    38.  
    39. Public Property Get ItemKEY(vntIndexKey As Variant) As String
    40.     ItemKEY = mCol(vntIndexKey)(1)
    41. End Property
    42.  
    43. Public Property Get Count() As Long
    44.     'used when retrieving the number of elements in the
    45.     'collection. Syntax: Debug.Print x.Count
    46.     Count = mCol.Count
    47. End Property
    48.  
    49. Public Sub Remove(vntIndexKey As Variant)
    50.     'used when removing an element from the collection
    51.     'vntIndexKey contains either the Index or Key, which is why
    52.     'it is declared as a Variant
    53.     'Syntax: x.Remove(xyz)
    54.    
    55.     mCol.Remove vntIndexKey
    56. End Sub
    57.  
    58. Public Property Get NewEnum() As IUnknown
    59.     'this property allows you to enumerate
    60.     'this collection with the For...Each syntax
    61.     Set NewEnum = mCol.[_NewEnum]
    62. End Property
    63.  
    64. Private Sub Class_Initialize()
    65.     'creates the collection when this class is created
    66.     Set mCol = New Collection
    67. End Sub
    68.  
    69. Private Sub Class_Terminate()
    70.     'destroys collection when this class is terminated
    71.     Set mCol = Nothing
    72. End Sub
    This is how you use it:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.     Dim ClsKey As New CollectionKEY, Itm As Variant
    5.     Dim K As Long
    6.    
    7.     ClsKey.Add "testing... 1", "KEY1"
    8.     ClsKey.Add "testing... 2", "KEY2"
    9.     ClsKey.Add "testing... 3", "KEY3"
    10.     ClsKey.Add "testing... 4", "KEY4", "KEY1"
    11.     ClsKey.Add "testing... 5", "KEY5", , "KEY2"
    12.    
    13.     For K = 1 To ClsKey.Count
    14.         Debug.Print ClsKey.Item(K), ClsKey.ItemKEY(K)
    15.     Next K
    16.    
    17.     Debug.Print
    18.    
    19.     ' using For Each
    20.     For Each Itm In ClsKey
    21.         Debug.Print Itm(0), Itm(1)
    22.     Next Itm
    23. End Sub

  6. #6

  7. #7
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: Displaying the "Key" in a Collection

    Quote 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:
    1. 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)...

  8. #8
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Displaying the "Key" in a Collection

    Quote 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...

  9. #9
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    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
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  10. #10
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    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
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  11. #11
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: Displaying the "Key" in a Collection

    Quote Originally Posted by Pradeep1210
    I faced a similar problem in the past and this is how it was resolved:
    [Resolved] How To Retrieve Keys from Collection?


    Pradeep
    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

  12. #12
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    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
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  13. #13
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Displaying the "Key" in a Collection

    Quote 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
  •  



Click Here to Expand Forum to Full Width