1. Technology

Visual Basic .NET 2010 Express - Collections of Things

Arrays, Collections, Lists, Tables, Stacks, Queues, and Dictionaries


In the program for this part of the tutorial, collections will play a key role. This upgrade to the Signature Block program adds the ability to keep more than one signature block in the XML file. Some serious XML programming is necessary to accomplish this trick. But first, we need to upgrade our knowledge of the various types of collections in .NET.

The Basic Array

The basic array is fast, efficient and easy to program. Declaring one is simply a matter of adding a parenthesis to contain one or more array dimension.

Dim myArray(d1, d2, ... , dn) as type

There are lots of variations. To declare an array of indefinite size, leave out the dimension. To initialize the array on declaration, add an array initialization. (MSDN documents these in detail. Part 2 tells you how to find this kind of detailed information.)

Here's an example: You might use an array if you are a teacher and you want a program to hold the names of four students in a study group:

Dim myStudents() As String =
    {"Jane", "Dick", "Tom", "Sam"}

Using this array, you can write code for all four students at once.

Dim theStudents As New StringBuilder
theStudents.Append("The students are: " & vbNewLine)
For i As Short = 0 To myStudents.Count - 1
    theStudents.Append(myStudents(i) & vbNewLine)

"But wait!" (I hear you shouting.) "That's a lot of code to simply display the names of four students. It would be easier to just put the name of each student in a variable by itself."

What if the same program was used for different groups of students and the program had to be used for arrays of different sizes?

Jagged Arrays

You can also use jagged arrays, where each element of an array is another array. Jagged arrays are not CLS - Common Language Specification - compliant and programmers sometimes find the syntax to use them difficult. The main benefit is that the storage space for a jagged array can be slightly less than a multidimensional array. So it could be that this type of array is more trouble than it's worth. I've added this type of array to the tutorial for completeness, not because it's a great idea.

Here's an example. The alternative to a jagged array is a multidimensional array. If you use a multidimensional array for the names of students for two classes of different sizes as shown below, one array element has to be empty. That's wasted space.

Dim myStudents(,) As String =
    {{"Dick", "Jane", "Tom", "Sam"},
     {"Sue", "Bill", "Mary", ""}}

There are only three students in the second group, so the last element is empty.

To display "Mary" using this multidimensional array, use this syntax:

MsgBox(myStudents(1, 2))

A jagged array avoids wasting space, but the cost is code syntax that is harder to understand.

Dim myStudents()() As String
myStudents =
    New String(1)() {
    New String() {"Dick", "Jane", "Tom", "Sam"},
    New String() {"Sue", "Bill", "Mary"}}

To display "Mary" from this jagged array, use this syntax:


The thing to remember about a jagged array is that it's really an array of arrays. So you have to deal with each dimension separately. In words, myStudents(1)(2) is "The third element of the second array." (Remember that array counters start at zero.) This can be confusing, especially if you just read the Microsoft MSDN documentation. Their example looks like this:

Dim sales()() As Double = New Double(11)() {}

If you try to initialize this array with a double nested loop, you discover that because only the highest dimension can be in the Dim statement, it won't work the way you might try to code it the first time. Most loops using jagged arrays require different coding. To initialize a jagged array ...

  1. Declare the jagged array and the first dimension.
  2. Use the outer loop to create and initialize each array in the next dimension.
  3. Repeat step 2 as required to initialize all the dimensions.

Here’s the code to do that for just two dimensions:

Dim myStudents()() As String _
   = New String(2)() {}
Dim myRand As New Random()
For i = 0 To 2
   myStudents(i) = _
      New String(myRand.Next(5)) {}
   For j = 0 To _
      myStudents(i).GetLength(0) - 1
      myStudents(i)(j) = "George"

Click here to display the illustration.

The length of the top level dimension is declared with the array. For example if the top level was "classes in the school" then you would just count the number of classes. In the example above, we assume there are three classes (0, 1, and 2). You would also need to get the length of the arrays that are in the lower level dimensions somehow too. In this example, I simply use a random number from 1 to 5. In the classroom example, the program would count the number of students in the different classes. Inside the loop, I assign all of the elements the same value but in a real-world case, you might assign some property, like the name, for a particular student.

Arrays have their own namespace in .NET, System.Array with lots of properties and methods. You might try using the Object Browser or Intellisense to browse through all the things you can do with arrays. The illustration below shows Intellisense.

Click here to display the illustration.

On the next page, we take a step up from arrays and learn about their more powerful cousins, Collections.

  1. About.com
  2. Technology
  3. Visual Basic
  4. Learn VB.NET
  5. Visual Basic .NET 2010 Express - Collections of Things

©2014 About.com. All rights reserved.