Add class to dictionary

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,834
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

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
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,215,844
Messages
6,127,252
Members
449,372
Latest member
charlottedv

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