joemeshuggah
Board Regular
- Joined
- Jul 18, 2008
- Messages
- 161
Sample code:
' clsCustomer Class Module Code
Public CustomerID As String
Public Amount As Long
Public Items As Long
Sub test()
Dim dict As New Scripting.Dictionary
Set oCust = New clsCustomer
' Set the values
oCust.CustomerID = "bob"
oCust.Amount = 1000
oCust.Items = 500
' Add the new clsCustomer object to the dictionary
dict.Add oCust.CustomerID, oCust
Dim key As Variant
For Each key In dict.Keys
Debug.Print dict(key).CustomerID
Debug.Print dict(key).Amount
Debug.Print dict(key).Items
Next key
End Sub
Two questions regarding above:
1. Is it possible to read through a dictionary without having to explicitly reference each item? Meaning, in the example above, instead of having to explicitly write out debug.print for CustomerID, Amount, and Items, is it possible to reference them via a loop through the items in clsCustomer? Something like:
Dim key As Variant
For Each key In dict.Keys
For Each x in clsCustomer
debug.print dict(key).x
Next x
Next key
2. Is it possible to associate a column header name to each item? Meaning, if I am writing the dictionary to a worksheet, when CustomerID is being written, I'd like the first row to show column header "Customer ID". I realize you can hardcode the column header and write the dictionary starting in row 2, but if the order of the dictionary items changes, the column headers would need to be manually re-ordered as well, which I want to avoid.
Thanks!
' clsCustomer Class Module Code
Public CustomerID As String
Public Amount As Long
Public Items As Long
Sub test()
Dim dict As New Scripting.Dictionary
Set oCust = New clsCustomer
' Set the values
oCust.CustomerID = "bob"
oCust.Amount = 1000
oCust.Items = 500
' Add the new clsCustomer object to the dictionary
dict.Add oCust.CustomerID, oCust
Dim key As Variant
For Each key In dict.Keys
Debug.Print dict(key).CustomerID
Debug.Print dict(key).Amount
Debug.Print dict(key).Items
Next key
End Sub
Two questions regarding above:
1. Is it possible to read through a dictionary without having to explicitly reference each item? Meaning, in the example above, instead of having to explicitly write out debug.print for CustomerID, Amount, and Items, is it possible to reference them via a loop through the items in clsCustomer? Something like:
Dim key As Variant
For Each key In dict.Keys
For Each x in clsCustomer
debug.print dict(key).x
Next x
Next key
2. Is it possible to associate a column header name to each item? Meaning, if I am writing the dictionary to a worksheet, when CustomerID is being written, I'd like the first row to show column header "Customer ID". I realize you can hardcode the column header and write the dictionary starting in row 2, but if the order of the dictionary items changes, the column headers would need to be manually re-ordered as well, which I want to avoid.
Thanks!