Welcome Guest!
Create Account | Login
Locator+ Code:

Search:
FTPOnline Channels Conferences Resources Hot Topics Partner Sites Magazines About FTP RSS 2.0 Feed



email article
printer friendly
more resources

Exploit Collections in VB
Take advantage of collections to both simplify and enhance your VB business applications.
by Daniel Clark

April 17, 2006

Technology Toolbox: Visual Basic

The .NET Framework contains an extensive set of classes and interfaces for creating and managing collections of objects. Taking advantage of these in your applications can help you build applications faster and more quickly.

ADVERTISEMENT

In particular, you should understand the various types of collection structures, what they are designed for, and when to use which. You should also learn how to use generics to create highly reusable, efficient collections.

I’ll walk you through the various types of collections exposed by the .NET Framework, including how to work with arrays and array lists, how to use hashtables and dictionaries, how to implement queues and stacks, and how to create strongly typed and generic-based collections.

Programmers need to work with collections frequently. For example, if you work with employee time records in a payroll system, you need to group the records by employee, loop through the records, and add up the hours for each.

All collections need a basic set of functionality, such as adding objects, removing objects, and iterating through their objects. In addition to the basic set, some collections need additional specialized functionality. For example, a collection of help-desk e-mail requests needs to implement a first-in, first-out functionality when adding and removing items from the collection.

The .NET Framework provides a variety of basic and specialized collection classes for you to use. The System.Collections namespace contains interfaces and classes that define various collections of objects, such as lists, queues, hashtables, and dictionaries (see Table 1 and Table 2).

An array is one of the simplest data structures in computer programming. An array holds data elements of the same data type. For example, you can create an array of integers, strings, or dates. You access the elements of an array through its index. The index is an integer representing the position of the element in the index. For example, an array of strings representing the days of the week has these index values: Sunday = 0, Monday = 1, Tuesday = 2, and so on. This days of the week example is a one-dimensional array, which means the index is represented by a single integer. Arrays can also be multidimensional. The index of an element of a multidimensional array is a set of integers equal to the number of dimensions. For example, look at this seating chart, which represents a two-dimensional array (see Figure 1).

You cannot explicitly derive from the Array class, but you implement array functionality when declaring an explicit type. For example, consider this code, which declares an array of type Integer:

Dim intArray() As Integer = {1, 2, 3, 4, 5}

You expose the properties and methods of the Array class once you declare the type as an array. Some of the functionality includes querying for the upper and lower bounds of the array, updating the elements of the array, and copying the elements of the array.

Declare and Manipulate Arrays
Declaring and working with an array of integers is straightforward (see Listing 1). You can also create multidimensional arrays. This code snippet declares and fills a two-dimensional array:

Dim int2DArray(2, 2) As Integer
For i As Integer = 0 To int2DArray.GetUpperBound(0)
   For x As Integer = 0 To _
     int2DArray.GetUpperBound(1)
     int2DArray(i, x) = i + x
   Next
Next
‘Print the index and value of the elements
For i As Integer = 0 To int2DArray.GetUpperBound(0)
   For x As Integer = 0 To _
   int2DArray.GetUpperBound(1)
   Console.WriteLine("index(" & i & "," & x & ")" 
     & ControlChars.Tab & "Value " & 
     int2DArray(i, x))
   Next
Next

You often don’t know the number of items contained in the collection until runtime when working with collections. You can resize an array using the Redim statement, but that isn’t an efficient or convenient way of working with a dynamic arrays. This is where the ArrayList class fits in. The capacity of an array list expands automatically as required, with the memory reallocation and copying of elements performed automatically. The ArrayList class also provides methods for adding, inserting, or removing a range of elements, which Array does not provide (see Listing 2).




Back to top














Java Pro | Visual Studio Magazine | Windows Server System Magazine
.NET Magazine | Enterprise Architect | XML & Web Services Magazine
| | Discussions | Newsletters | FTP Home