Solved

Declare collection at runtime

Posted on 2002-05-23
8
181 Views
Last Modified: 2010-05-02
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
0
Comment
Question by:bgumble
8 Comments
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 7029336
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.
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 7029427
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"
0
 

Author Comment

by:bgumble
ID: 7029531
Hi Tim,

Thank you for your suggestion.

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

Thanks
0
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 

Author Comment

by:bgumble
ID: 7029541
Hi Tim,

Thank you for your suggestion.

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

Thanks
0
 
LVL 6

Expert Comment

by:holli
ID: 7029689
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
0
 

Author Comment

by:bgumble
ID: 7030881
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

0
 

Author Comment

by:bgumble
ID: 7030918
Actually, what I really need to know is how to add an element to the collection within the collection

thanks..
0
 
LVL 6

Accepted Solution

by:
holli earned 50 total points
ID: 7031444
just as if the collection would be single:

myCollectionOfCollections.Item("Name").Add "Key2", NewValue
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question