(Solved) – VBA: Loop over Items in Dictionary with an Object Variable

(solved)-–-vba:-loop-over-items-in-dictionary-with-an-object-variable

I am trying to loop over the items in a dictionary with an object variable that refer to inheritance class “Breed”, but I am unable to do so with dictionaries but with collections it is pretty simple is there a way to solve this without using the dictionary’s keys? because then I will lose the ability to use the intelisense feature.

Here is the code of the class Breed:

Option Explicit

Public Property Get Name() As String
End Property

Public Property Get Color() As String
End Property

Public Property Get Price() As Double
End Property

Here is the code for class Dogs:

Option Explicit

Implements Breed

Private pName As String, pPrice As Double, pColor As String

Public Property Let Name(Val As String)
    pName = Val
End Property

Public Property Get Name() As String
    Name = pName
End Property

Private Property Get Breed_Name() As String
    Breed_Name = Name
End Property

'''''''''''''''''''''''''''''''''''''''''''''''''''''

Public Property Let Price(Val As Double)
    pPrice = Val
End Property

Public Property Get Price() As Double
    Price = pPrice
End Property

Private Property Get Breed_Price() As Double
    Breed_Price = Price
End Property

'''''''''''''''''''''''''''''''''''''''''''''''''''''

Public Property Let Color(Val As String)
    pColor = Val
End Property

Public Property Get Color() As String
    Color = pColor
End Property

Private Property Get Breed_Color() As String
    Breed_Color = Color
End Property

Here is the code for class Cats:

Option Explicit

Implements Breed

Private pName As String, pPrice As Double, pColor As String

Public Property Let Name(Val As String)
    pName = Val
End Property

Public Property Get Name() As String
    Name = pName
End Property

Private Property Get Breed_Name() As String
    Breed_Name = Name
End Property

'''''''''''''''''''''''''''''''''''''''''''''''''''''

Public Property Let Price(Val As Double)
    pPrice = Val
End Property

Public Property Get Price() As Double
    Price = pPrice
End Property

Private Property Get Breed_Price() As Double
    Breed_Price = Price
End Property

'''''''''''''''''''''''''''''''''''''''''''''''''''''

Public Property Let Color(Val As String)
    pColor = Val
End Property

Public Property Get Color() As String
    Color = pColor
End Property

Private Property Get Breed_Color() As String
    Breed_Color = Color
End Property

Here is the code for the regular module with collection but fails with dictionary:

Option Explicit

Sub Main()

    Dim C           As Cats
    Dim D           As Dogs
    Dim Coll        As Collection
    Dim B           As Breed

    Set C = New Cats
    C.Name = "Catomon"
    C.Color = "Angle White"
    C.Price = 800.98

    Set D = New Dogs
    D.Name = "Dogomon"
    D.Color = "Golden White"
    D.Price = 1000.23

    Set Coll = New Collection
    Coll.Add C
    Coll.Add D

    Set B = New Breed

    For Each B In Coll
        Debug.Print B.Name, B.Color, B.Price
    Next B

    Set C = Nothing
    Set D = Nothing
    Set B = Nothing
    Set Coll = Nothing

End Sub

Leave a Reply

Your email address will not be published. Required fields are marked *