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
AlphaSquaredAsked:
Who is Participating?
 
mvidasConnect With a Mentor Commented:
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

Matt
0
 
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
0
 
AlphaSquaredAuthor Commented:
Brilliant!  Fast, simple, and elegant.  Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.