• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 148
  • Last Modified:

Naming an object with a variable and adding it to a collection

I need to:
   1.  create object,
   2.  give it a name related to a name of an existing object in an existing collection (i.e. Bounce1, Bounce2, ... and then
   3.  add the object to the existing collection

I've tried with the following:

Public Sub AddItemToDetailsCollection()
    Dim objectName As String
    objectName = "Bounce" + Str(Details.Count + 1)    ' Details is an existing collection
    Dim objectName As New TradeDetail
    Details.Add Item:=objectName
End Sub

Many many thanks.  A
1 Solution
Hi AA,

I'm assuming TradeDetail is a class or udt, does it have a Name property?  If so, you could do something like:

    Dim objectName As New TradeDetail
    objectName.Name = "Bounce" + CStr(Details.Count + 1)
    Details.Add Item:=objectName

Mike TomlinsonMiddle School Assistant TeacherCommented:
Are trying to add an item to your details collection with a key so you can retrieve them later using "Bounce1", "Bounce2", etc...?

Public Sub AddItemToDetailsCollection()
    Dim keyValue As String
    keyValue = "Bounce" + Trim(Str(details.Count + 1))
    Debug.Print keyValue
    Dim obj As New Tradedetail
    details.Add obj, keyValue
    ' to get it back out...
    Dim retrievedObj As Tradedetail
    Set retrievedObj = details("Bounce1")
    ' do something with retrievedObj...
End Sub
AlphaSquaredAuthor Commented:
Brilliant!  Fast, simple, and elegant.  Thanks

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now