Microsoft Visual Basic 2010 Step by Step eBook

(Tina Meador) #1
273

Chapter 11


Using Arrays to Manage Numeric


and String Data


After completing this chapter, you will be able to:
n Organize information in fixed-size and dynamic arrays.
n Preserve array data when you redimension arrays.
n Use arrays in your code to manage large amounts of data.
n Use the Sort and Reverse methods in the Array class to reorder arrays.
n Use the ProgressBar control in your programs to show how long a task is taking.

Managing information in a Microsoft Visual Basic application is an important task, and as
your programs become more substantial, you’ll need additional tools to store and process
data. A quick-and-dirty approach to data management in programs is to store and retrieve
information in auxiliary text files, as you’ll see in Chapter 13, “Exploring Text Files and
String Processing .” However, the most comprehensive approach is storing and retrieving
information by using databases, and you’ll start learning how to integrate Visual Basic
programs with databases in Chapter 18, “Getting Started with ADO .NET .”

In this chapter, you’ll learn how to organize variables and other information into useful
containers called arrays. You’ll learn how to streamline data-management tasks with
fixed-size and dynamic arrays and how to use arrays in your code to manage large amounts
of data. You’ll learn how to redimension arrays and preserve the data in arrays when you
decide to change an array’s size. To demonstrate how large arrays can be processed, you’ll
use the Sort and Reverse methods in the Microsoft .NET Framework Array class to reorder
an array containing random six-digit integer values. Finally, you’ll learn to use the ProgressBar
control to give your users an indication of how long a process (array-related or otherwise) is
taking. The techniques you’ll learn provide a solid introduction to the database programming
techniques that you’ll explore later in the book.

Working with Arrays of Variables


In this section, you’ll learn about arrays, a useful method for storing almost any amount
of data during program execution. Arrays are a powerful and time-tested mechanism for
storing logically related values in a program. The developers of BASIC, Pascal, C, and other
popular programming languages incorporated arrays into the earliest versions of these
products to refer to a group of values by using one name and to process those values
individually or collectively.

Table of Contents


Using Arrays to Manage Numeric and String Data.............. 273


Working with Arrays of Variables..................................... 273
Creating an Array.............................................. 274
Declaring a Fixed-Size Array.................................... 275
Setting Aside Memory......................................... 276
Working with Array Elements................................... 277
Declaring an Array and Assigning It Initial Values.................. 278
Creating a Fixed-Size Array to Hold Temperatures................. 279
Creating a Dynamic Array...................................... 283
Preserving Array Contents by Using ReDim Preserve................. 287
Using ReDim for Three-Dimensional Arrays....................... 288
One Step Further: Processing Large Arrays
by Using Methods in the Array Class.................................. 288
The Array Class............................................... 288
Chapter 11 Quick Reference.......................................... 295
Free download pdf