The Visual Basic Collection Class

We’ve discussed arrays at length in the tutorials on one dimensional arrays, multi dimensional arrays and jagged arrays for you Visual Studio developers.  Arrays are an extremely powerful means to store linked data, but there are 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?  Often, when we’re choose the next 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.   In this case, when we need to store a list of data arrays will be a natural and obvious candidate for the solution.  Arrays provide a simple, quick means to store and iterate structures of 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 and in those case you should really evaluate if the Array is the best tool to solve your problem.   If you need to create 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 an 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 datatypes.   However the Collection class is slower than other collections out there, and it offers less functionality than others still.  It does provide backwards compatibility though and is available to Visual Basic 6, VBA and VB.NET developers.

Remember my list of Visual Basic books that we looked at during the tutorial of arrays?  I don’t have so many books, but I want to add to this list whenever I see fit.  I have 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

collectionVariable.Items Add(object)

or

collectionVariable.Add(object)

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, which is 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

visual-basic-collections-1

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

Which adds all items in our passed Collection collectionBooks to the Listbox ListBoxBooks.

Run the Project and you’ll see all the books in the collection listed. Single step 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 and then iterating the collection – adding the items to a listbox. In the next tutorial we’ll look at how we can manipulate and sort the data in the collection.