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

Declare collection at runtime

Hi I am new to Visual Basic.

I can declare a collection in Visual Basic by

Dim mybooks As New Collection

however at run time I would like to create new collections
which a user may type in.

so I woudl like to

dim ("whatever usertypes in") as new collection

and use

"whatever usertypes in" as a normal object name.

Thanks for any help.

1 Solution
Éric MoreauSenior .Net ConsultantCommented:
Collections are compiled so you can create them at run-time. If you are using collections to store regular data (integer, date, string, ...), you can use disconnected ADO Recordset instead of collection. Recordsets can exists without a database and you can add them at run-time.
There is no way that you are going to get something like this. You could create an array of collections so that you could reference an element in a collection using something like:

aryCollections(arrayElement)(CollectionElement).Property = "Hello"
bgumbleAuthor Commented:
Hi Tim,

Thank you for your suggestion.

Could you please give me and example on how to do this.

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

bgumbleAuthor Commented:
Hi Tim,

Thank you for your suggestion.

Could you please give me and example on how to do this.

Since a collection can hold all types, it can also handle other collections, so:

'A collection to hold the collections
dim myCollectionOfCollections as new collection

'The member to add
dim myCollectionMember as Collection

'Adding a collection
set myCollectionMember = new collection
myCollectionMember.add "Value", "Key"
myCollectionOfCollections.add myCollectionMember,s

'Dereferencing a collection
v=myCollectionOfCollections.Item("Name").Item("Key") 'v is now "value"

regards, holli
bgumbleAuthor Commented:
Hi Holli

Thanks for the help..

That all works

however I can't seem to easly add an item to the collection that is within the collection..

I've tried

myCollectionOfCollections.Item("Name").Item("Key") = newvalue

bgumbleAuthor Commented:
Actually, what I really need to know is how to add an element to the collection within the collection

just as if the collection would be single:

myCollectionOfCollections.Item("Name").Add "Key2", NewValue
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.

Join & Write a Comment

Featured Post

Train for your Pen Testing Engineer Certification

Enroll today in this bundle of courses to gain experience in the logistics of pen testing, Linux fundamentals, vulnerability assessments, detecting live systems, and more! This series, valued at $3,000, is free for Premium members, Team Accounts, and Qualified Experts.

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