Class x Property as Collection?

EB08

Active Member
Joined
Jun 9, 2008
Messages
343
In generic terms, i have a class named 'Client' with read/write properties of 'ID', 'Alias', and 'Plan'. The main issue i'm running into right now is how to handle the 'Plan' property; each client can have no less than 1, but up to a maximum of 15 plans. Ideally, i would like to treat the property 'Plan' like a collection in which i could add, remove, and count items.

I'm currently using a collection (Clients) to manage each instance of the class 'Client', but need a way to give each 'Client' a dynamic number of plans...any help or direction would be greatly appreciated.
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Hi,
Put the code below into Client class module:
Rich (BB code):

' Code of the Client class module
Option Explicit
Dim MyCol As New Collection

Public Property Get Plan() As Collection
  Set Plan = MyCol
End Property

Private Sub Class_Terminate()
  Set MyCol = Nothing
End Sub

And call it via Module1 code like this:
Rich (BB code):

' Code of Module1
Sub Test()
  Dim x As New Client
  With x.Plan
    .Add 1, "one"
    .Add 2, "two"
    Debug.Print "Count=" & .Count, "one=" & .Item("one"), "two" & .Item("two")
  End With
End Sub
 
Upvote 0
Perfect...exactly what i was looking for, thanks.

Is it necessary to have the Class_Terminate, or, is that just a result of best-practices?
 
Upvote 0
Perfect...exactly what i was looking for, thanks.
Glad it helped!

Is it necessary to have the Class_Terminate, or, is that just a result of best-practices?
It’s just the the habit to help VBA :)
Setting to nothing is required for object variables for avoiding of Excel memory leakage.
But this is not the rule for Collections embedded into VBA.

Regards,
Vlad
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,537
Messages
6,179,405
Members
452,911
Latest member
a_barila

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