Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding variable number of objects to collection

Tags:

oop

vba

I understand adding objects to a collection can be done by

Dim ItemList As Collection
Set ItemList = New Collection

Dim Item As New CItem

Set Shoe = New CItem
      With Shoe
           .Quantity
           .IDNumber 
           .Description
      End With
ItemList.Add Shoe

Set Bag = New CItem
      With Bag
           .Quantity
           .IDNumber 
           .Description
      End With
ItemList.Add Bag

I would be able to call data i want to use like (Cost = Bag.Quantity * 2)

However my problem is that my list of items will be user defined. Is there any way to add a variable number of objects into a collection and still be able to retrieve individual data by the item name?

for example, i am given a list of items: Shoe, Bag, Sunglasses, Pants

I would like to write a for loop to read all these objects under the class "Item" but still be able to calculate (xyz = Sunglasses.Quantity + Pants.Quantity - Bag.Quantity). I have tried to use counters, but it seems to only accept constant expressions.

Is this possible? If so, i would appreciate help in finding out how to do it.

like image 661
Jonathan Tan Avatar asked Nov 28 '25 23:11

Jonathan Tan


1 Answers

You can add an item to collection with key:

ItemList.Add Shoe, "Shoe"

and then retrieve it by specifying this key:

Dim Shoe As CItem
Set Shoe = ItemList.Item("Shoe")
like image 80
mielk Avatar answered Dec 01 '25 12:12

mielk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!