1. Home
  2. Computing & Technology
  3. Visual Basic

Visual Basic .NET 2005 Express - Collections of Things

The Array on Steroids: Collections

By Dan Mabbutt, About.com

Mar 1 2008

In addition to arrays, other collections of objects are in the System.Collections namespace to provide some really advanced functions like sorting, fast retrieval even with large objects, and LIFO and FIFO access to the collection (last in, first out and first in, first out).

In general, the way you work with a collection is quite different than an array. As a starting example, you add something to an array using the simple assignment operator:

Dim myStudentArray(10) As String
myStudentArray(5) = "George"

But with a collection, you must use the Add method (and the Remove method to delete an element again).

Dim myStudentColl As New Collection
myStudentColl.Add("George")

And there are a few other differences!

First, we don't have to tell VB Express how big the collection is. If you use an array and then change the size, you have to use the ReDim statement if the number of elements in the array ever gets bigger than the initial declaration. But collections are inherently variable in size.

Second, we don't have to tell VB Express that the collection would hold String members. Collections are "weakly typed". So we can use a variety of different data types together in the same collection. This goes back to the concept used in VB6 where most data was a Variant data type. As a result, Collections aren't as fast as arrays and don't protect you from using data that isn't in the correct type.

Here's an example:

Dim myStudentColl As New Collection
myStudentColl.Add("George")
myStudentColl.Add(1234567)
Debug.WriteLine(myStudentColl(1))
Debug.WriteLine(myStudentColl(2))

Notice also that the index of the first element is 1 for collections and 0 for arrays. (For reasons that have never made sense to me! Space is allocated for the zero element of a collection, but you can't use it. See the illustration below.)

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

A subtle point that the new collection coding syntax implies is that you can't update either. In other words, with an array, you can update the value saved in the sixth element like this:

myStudentArray(5) = "George"
myStudentArray(5) = "Mary"

But you can only Add or Remove the elements in a collection. There is no "update" or "change" method.

If you want to iterate over all of the elements in a collection, use the For Each syntax:

For Each student As String In myStudentColl
   Debug.WriteLine(student)
Next student

Microsoft summarizes the things you can do with a collection in this list:

  • Add an element with the Add Method.
  • Remove an element with the Remove Method.
  • Remove all elements with the Clear Method.
  • Find out how many elements the collection contains with the Count Property.
  • Check whether a specific element is present with the Contains Method.
  • Return a specific element from the collection with the Item Property.
  • Iterate through the entire collection with the For Each...Next Statement.

Another thing to keep in mind is that collections are re-indexed every time something is added or removed. (That's why you don't have to tell VB.NET how big they are.) This has an interesting result when you're removing thing as the code below shows:

Dim myStudentColl As New Collection
myStudentColl.Add("George")
myStudentColl.Add(1234567)
Debug.WriteLine(myStudentColl(1))
' Result is "George"
myStudentColl.Remove(1)
' collection is re-indexed
Debug.WriteLine(myStudentColl(1))
' Result is 1234567

Arrays and Structures

Using both arrays and collections, you can take advantage of another VB.NET idea to keep different types of data in the same grouping: structures. A structure plays a key role in the Signature Block program.

Let's first create a structure:

' This block must be coded before
' a Sub or Function statement
Structure StructObj
   Dim StringItem As String
   Dim NumberItem As Integer
   Dim DateItem As Date
End Structure

Here's the way it would be used with an array:

Private Sub mysub( ...    Dim myStudentArray(10) As StructObj    myStudentArray(5).StringItem = "George"    myStudentArray(5).NumberItem = 123456    myStudentArray(5).DateItem = #12/7/1941#

And here's a similar idea with a collection:

Dim myStudentColl As New Collection
Dim myStructObj As StructObj
myStructObj.StringItem = "George"
myStructObj.NumberItem = 123456
myStructObj.DateItem = #12/7/1941#
myStudentColl.Add(myStructObj)

The bottom line is that collections are for the more demanding tasks in your program. So let's see some of the new tricks offered by collections!

Using Keys to Access Collections

Another feature of collections can also be seen in the illustration below.

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

Note that each member of the collection has a key (whether you want it or not). You can access collections like a simple database using the key value. The key must be a string and it must be unique. So if you run this code:

Dim myStudentColl As New Collection
myStudentColl.Add("George", "ID-1")
myStudentColl.Add("Mary", "ID-2")
myStudentColl.Add("Sam", "ID-3")
myStudentColl.Add("Tom", "ID-4")
Debug.WriteLine(myStudentColl.Item("ID-3"))

The result is "Sam".

The family of objects that are based on collections is one of the most powerful aspects of collections. These include ArrayList, Stack, Queue, Dictionary and Hashtable. We learn about those next.

Explore Visual Basic

More from About.com

  1. Home
  2. Computing & Technology
  3. Visual Basic
  4. Learn VB.NET
  5. Visual Basic .NET 2005 Express - Collections of Things

©2008 About.com, a part of The New York Times Company.

All rights reserved.