We’ve discussed arrays at length in previous tutorials. Arrays, if you were paying attention, provide an extremely powerful means to store linked data. However Visual Basic offers many alternatives to arrays. In this, the first of four tutorials, we are going to explore one of these alternatives, namely the Visual Basic Collection Class.
So why would we use these other collections in Visual Basic rather than an array? Well there’s no right and wrong answer to this question. Do you know the phrase, there’s more than one way to skin a cat? When we choose the best solution to a given problem, we’ll have to evaluate the advantages and disadvantages of any approach. Sometimes the “best” answer comes down to personal preference. For example, when we need to store a list of data, arrays will be a natural and obvious candidate for the solution. Arrays provide a simple and quick means to store and iterate structures of data. There is one caveat; arrays provide a simple means to store data when the size of the structure is known in advance. If you don’t know the size of the structure, using the Redim Statement is an expensive hog of your system resources. Redim Preserve (to preserve the contents of the array while resizing) even more so. In many cases, you will not know the size of the list in a given situation. In these cases you should really consider if the array is the best tool to solve your problem. If you need to manage a list without the knowing the size in advance, there are other Collections in Visual Basic that could do a better job? For example, the Visual Basic Collection Class, allows you to add and remove items dynamically without knowing the size of the list in advance. It’s also quicker to iterate than an array, significantly so if you have 10,000 or more items in the list. In addition, unlike an array, the Collection class can store items of differing data types. However the Collection class is slower than other collections out there and it offers less functionality than others still. It does provide backwards compatibility to VB6 and is familiar to Visual Basic 6, VBA and VB.NET developers.
Remember my list of Visual Basic books that we looked at during the tutorial on arrays? I want to add to this list whenever I see fit so I’ve decided to store them in a Visual Basic Collection Class.
The books are:
- Microsoft Visual Basic 2010 Step By Step
- Sams Teach Yourself Visual Basic 2012 in 24 Hours, Complete Starter Kit
- Distributed Applications with Microsoft Visual Basic 6.0 McSd Training Kit : For Exam 70-175
- Microsoft® ASP.NET Programming with Microsoft Visual Basic® .NET Version 2003 Step By Step
- Visual Basic 6 Design Patterns
- Excel VBA Programming For Dummies
- Learn to Program with Visual Basic
- Visual Basic 6 Complete
To create a new Visual Basic Collection Class use the following syntax:
Dim variablename as New Collection
The syntax to add items to a collection is
We will use the latter as it is shorthand and easier. It is worth noting that for a collection you can add items of any data type. You can also mix up the types of data in the collection, one item could be an integer and the next a string providing a very powerful advantage over arrays.
To try this out, create a new Visual Basic Project called Collection Class and follow these instructions:
- In Form1 add a new ListBox
- Set the Name property to ListBoxBooks
- Resize the ListBox so it looks something like this
Add the following code in the Form_Load event:
Dim bookCollection As New Collection bookCollection.Add("Microsoft Visual Basic 2010 Step By Step") bookCollection.Add("Sams Teach Yourself Visual Basic 2012 in 24 Hours, Complete Starter Kit") bookCollection.Add("Distributed Applications with Microsoft Visual Basic 6.0 McSd Training Kit : For Exam 70-175") bookCollection.Add("Microsoft® ASP.NET Programming with Microsoft Visual Basic® .NET Version 2003 Step By Step") bookCollection.Add("Visual Basic 6 Design Patterns") bookCollection.Add("Excel VBA Programming For Dummies") bookCollection.Add("Learn to Program with Visual Basic") bookCollection.Add("Visual Basic 6 Complete") AddBooksToListBox(bookCollection)
Here we’re adding my Visual Basic book collection to the Collection Class called bookCollection.
To iterate the collection and add the books to ListBox, create a new Subroutine called AddBooksToListBox in the Form with the following code:
Private Sub AddBooksToListBox(ByRef collectionBooks As Collection) For i = 1 To collectionBooks.Count ListBoxBooks.Items.Add(collectionBooks.Item(i).ToString) Next End Sub
This code loops through all items in collectionBooks, adding them to the Listbox ListBoxBooks.
Run the Project and you’ll see all the books in the collection listed. Try single stepping through the code so you can get a feel for what is happening here. Essentially we’re creating a Collection, adding items to the collection, iterating the collection and finally displaying the items in a ListBox. In the next tutorial we’ll build on this code and look at how we can manipulate and sort the data in the collection.