Chapter 14. Arrays

   

Most of the examples in previous chapters have dealt with one object at a time. In many applications, however, you will want to work with a collection of objects all at the same time. The simplest collection in VB.NET is the array , which this chapter covers in detail. More complicated collection classes, such as Stack and Queue, are covered in the next chapter.

In this chapter, you will learn to work with three types of arrays: one-dimensional arrays, multidimensional rectangular arrays, and multidimensional jagged arrays.

To picture a one-dimensional array, imagine a series of mailboxes, all lined up one after the other. Each mailbox can hold exactly one object (one letter, one box, etc.). It turns out that all the mailboxes must hold the same kind of object; you declare the type of object the mailboxes will hold when you declare the array.

A multidimensional array allows you to create rows of mailboxes, one above the other. If all the rows are the same length, you have a rectangular array. If each row of mailboxes is a different length, you have a jagged array.

You can think of a multidimensional array as being like a grid of rows and columns in which each slot (mailbox) contains information. For example, each column might contain information pertinent to an employee. Each row would contain all the information for a single employee.

Most often you will deal with one-dimensional arrays, and if you do create multidimensional arrays, they will be two-dimensional ”but larger multidimensional arrays (3-D, 4-D, etc.) are also possible.

A jagged array is a type of two-dimensional array in which each row can have a different number of columns. A jagged array is less of a grid, and more of an array of arrays ”that is, an array in which the elements are arrays. This allows you to group a few arrays of varying sizes into a single array. For example, you might have an array of ten buttons , and a second array of five listboxes, and a third array of seven checkboxes. You can group all three into a jagged array of controls.

The current chapter also introduces the concept of indexers, a feature of VB.NET that makes it possible to create your own classes that can be treated like arrays.

   


Learning Visual Basic. NET
Learning Visual Basic .Net
ISBN: 0596003862
EAN: 2147483647
Year: 2002
Pages: 153
Authors: Jesse Liberty

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net