Add class to dictionary

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,924
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
In the following code to find unique values and aggregate amounts and volumes:

Code:
Option Explicit
Sub Start()
    Dim dict As Scripting.Dictionary
Set dict = ReadData

End Sub

Function ReadData() As Dictionary
    Dim dict As Scripting.Dictionary
Set dict = New Scripting.Dictionary

Dim rg As Range
Set rg = Sheet1.Range("A1").CurrentRegion

Dim i As Long, CustomerID As String, Amount As Currency
Dim Volume As Long, oCustomer As Class1

For i = 2 To rg.Rows.Count

CustomerID = rg.Cells(i, 1).Value

Amount = rg.Cells(i, 4).Value
Volume = rg.Cells(i, 5).Value

If dict.Exists(CustomerID) Then

Set oCustomer = dict(CustomerID)

Else

Set oCustomer = New Class1

dict.Add CustomerID, oCustomer

End If

oCustomer.Amount = oCustomer.Amount + Amount
oCustomer.Volume = oCustomer.Volume + Volume

    Next i
    Set ReadData = dict

End Function


This is Class1:

Code:
Option Explicit

Private pAmount As Currency
Private pVolume As Long
Public Property Get Amount() As Currency
    Amount = pAmount

End Property
Public Property Let Amount(ByVal vNewValue As Currency)
    pAmount = vNewValue
End Property
Public Property Get Volume() As Long
    Volume = pVolume

End Property
Public Property Let Volume(ByVal vNewValue As Long)
    pVolume = vNewValue

End Property

can someone please explain this line:

Code:
If dict.Exists(CustomerID) Then
Set oCustomer = dict(CustomerID) 'PUZZLES ME ?????
Else
Set oCustomer = New Class1
dict.Add CustomerID, oCustomer
End If

I understand that if the dictionary with the key CustomerID DOES NOT exist, then create a new instance of Class1 and add this instance of Class1 to the dictionary with CustomerID as the key but if the key CustomerID DOES exist, what is "Set oCustomer = dict(CustomerID)" doing?

Thanks
 
Actually, the dictionary object could have been declared at the module level, and then both procedure and function, along with any other procedures or functions in the module, would have direct access to it. So there would be no need to pass the dictionary object from the function back to the calling procedure. However, it's considered poor programming practice. :)
Why poor? Thought it would be more efficient or is it because it's advisable to separate different components in programming?
 
Upvote 0

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
With global variables, their values can be changed by any procedure or function that is called. This can lead to unpredictable results. Local variables, on the other hand, are much safer because other procedures and functions can't affect them directly. But there other reasons as well. You may want to search Google for additional information.
 
Upvote 0
With global variables, their values can be changed by any procedure or function that is called. This can lead to unpredictable results. Local variables, on the other hand, are much safer because other procedures and functions can't affect them directly. But there other reasons as well. You may want to search Google for additional information.
Thanks
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,186
Members
452,615
Latest member
bogeys2birdies

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