Link to home
Start Free TrialLog in
Avatar of bgumble
bgumble

asked on

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.

Ber
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

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"
Avatar of bgumble
bgumble

ASKER

Hi Tim,

Thank you for your suggestion.

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

Thanks
Avatar of bgumble

ASKER

Hi Tim,

Thank you for your suggestion.

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

Thanks
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
s="Name"
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
Avatar of bgumble

ASKER

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

Avatar of bgumble

ASKER

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

thanks..
ASKER CERTIFIED SOLUTION
Avatar of holli
holli

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial