Class Collections - How do I set the key?

MrKowz

Well-known Member
Joined
Jun 30, 2008
Messages
6,653
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Wracking my brain on this one.

I have a class module which is populating various attributes of a list and is storing them in a collection. The collection is being built just fine, but I'm unable to figure out why the heck the collection key isn't working, despite me explicitly defining it. Have even tried defining "oldColl.Add NewProduct, key:=NewProduct.PDCN", but to no avail.

Example:

This snippet of code builds the NewProduct class object. The Contains() function returns FALSE here, despite me explicitly defining the key in the line before it. I'm expecting it to return TRUE.

Code:
                Set NewProduct = New cNewProducts
                NewProduct.Product = tmpProdName
                NewProduct.Package = .Range("B" & i).value
                NewProduct.PDCN = .Range("C" & i).value
                oldColl.Add NewProduct, NewProduct.PDCN
                MsgBox Contains(oldColl, NewProduct.PDCN)

Contains() Function:
Code:
Public Function Contains(col As Collection, key As Variant) As Boolean
Dim obj As Variant
On Error GoTo err
obj = col(key)
Contains = True
On Error GoTo 0
Exit Function
    
err:
    Contains = False
End Function

I'd appreciate any help here. Thanks!
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
The data type of a key for a collection is String. If the PDCN property is a number, then that number is converted to a string; "42", not 42.

If you pass a number as the key to Contains, then it it trying to find the item by index (42), not key ("42").
 
Upvote 0
The data type of a key for a collection is String. If the PDCN property is a number, then that number is converted to a string; "42", not 42.

If you pass a number as the key to Contains, then it it trying to find the item by index (42), not key ("42").

PDCN is a string (example value: "1NM7G") and is defined as such in the class code:

Code:
Private pPDCN           As String
Public Property Get PDCN() As String
    PDCN = pPDCN
End Property

Public Property Let PDCN(value As String)
    pPDCN = value
End Property
 
Last edited:
Upvote 0
That would have been helpful to know earlier, since I hate typing, MrK.

In that case, how about ...

Code:
[COLOR="#FF0000"]Set[/COLOR] obj = col(key)
 
Upvote 0
That would have been helpful to know earlier, since I hate typing, MrK.

In that case, how about ...

Code:
[COLOR="#FF0000"]Set[/COLOR] obj = col(key)

Thanks - that actually ended up being the problem! Appreciate you!
 
Upvote 0

Forum statistics

Threads
1,216,110
Messages
6,128,892
Members
449,477
Latest member
panjongshing

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top