In the program for this segment, collections will play a key role. The program is a significant upgrade of the Signature Block program that adds the ability to keep more than one signature block in the XML file that is used to save the blocks. 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.)
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"}
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.
For example, this multidimensional array will save the names of students for two classes, but note that one array element has to be empty.
Dim myStudents(,) As String = _
{{"Dick", "Jane", "Tom", "Sam"}, _
{"Sue", "Bill", "Mary", ""}}
To display "Mary" using this multidimensional array, use this syntax:
Debug.WriteLine(myStudents(1, 2))
This jagged array avoids allocating storage for exactly one string element. 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:
Debug.WriteLine(myStudents(1)(2))
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. 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 doesn't work the way you expect it to. Most loops using jagged arrays require coding that is different than you might use otherwise before they work.
To initialize a jagged array ...
- Declare the jagged array and the first dimension
- Use the outer loop to create and initialize each array in the next dimension
- Repeat step 2 as required to initialize all the dimensions
Heres the code to do that (for 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"
Next
Next
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. It's useful to use the Object Browser or Intellisense to browse through all the things you can do.
--------
Click Here to display the illustration
Click the Back button on your browser to return
--------
On the next page, we take a step up from arrays and learn about their more powerful cousins, Collections.

