Arrays and Collections in Visual Basic .NET

Chapter 6

Arrays and Collections in Visual Basic .NET

In most programs, you need to store information in memory. For example, when you dimension a variable in a statement such as Dim sMyString as string = "Hello Visual Basic .NET", you have stored a string in memory. Many times, you'll need to store several strings or numbers together. Let's say you wanted to store a list of cities in the United States so that they could be accessed readily in your program. Most classic Visual Basic programmers would use an array to do this.

In most languages, arrays are the simplest and most common type of structured data. Arrays are handled differently in Visual Basic .NET than in classic Visual Basic. As you probably know, an array is a reference data type that contains variables accessed through indexes. The variables contained in an array, called the elements of the array, must all be of the same type, such as all integers or all strings. The elements of an array are created when an instance of the array is created, and, of course, they are extinguished when the instance of the array is destroyed. Each element of an array is initialized to the default value of its type. Because arrays are reference objects, they are allocated out of heap space rather than from the stack.

Arrays allow you to refer to a series of variables by the name of the array and to use a number, called an index or subscript, to tell them apart. Conceptually an array is like a row of mailboxes, each with a sequential number holding one item of the data type declared with the array. Arrays help you create shorter and simpler code in many situations because you can set up loops that deal efficiently with any number of elements by using the index number.

Collections, on the other hand, are classes that provide controlled access to specific types of variables or objects. For example, Visual Basic .NET has several built-in collections of controls, printers, and forms. If you want to program Microsoft Excel from Visual Basic, you can create an instance of an Excel object and access its collections via Excel's object model. You can create a workbook, grab a worksheet from the worksheet collection, and locate a cell in the worksheet's cell collection. Visual Basic 5 was the first version to provide a built-in collection data type that programmers could use.

When you place an element in a certain position in an array, you can be sure that the element will always be stored in the same position. Collections grow and shrink dynamically, as if they were an array we never have to redimension. While arrays are ordered, collections are unordered—an element's location within the collection can change. Therefore, you usually want to retrieve a collection element by using a key rather than a specific location within the collection. Arrays and collections have other differences that I'll cover as we move through the chapter.



Coding Techniques for Microsoft Visual Basic. NET
Coding Techniques for Microsoft Visual Basic .NET
ISBN: 0735612544
EAN: 2147483647
Year: 2002
Pages: 123
Authors: John Connell

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