WORKING WITH ARRAYS


It can quickly become difficult to keep up with large numbers of variables. Many times, you'll find that the different pieces of data collected and processed by a Visual Basic program have a strong relationship to one another. For example, you might write an application that collects and processes a list of people's names. In this type of situation, you can organize and manage all the names collected by your application as a unit in an array.

Defining Arrays

You can use the Dim keyword to create a single-dimension array using the following syntax.

 Dim ArrayName(dimensions) As Integer 

image from book
DEFINITION

An array is an Indexed list of data. The first piece of data stored in an array is assigned an index position of 0. The next piece of data is assigned an index position of I, and so on. Any data stored in the ;array can be referenced based on its index position.

image from book

Dimensions represents a comma-separated list of numbers that specify how many dimensions make up the array. For example, the following statement defines an array named strCustomerNamesArray that can hold up to five names:

 Dim  StrCustomerNamesArray(4) as String 

Because an array begins with an index number of 0, I specified the number 4 in order to set the array up to hold five entries (0 through 4).

Hint 

You may have noticed that in the previous example, I added the word Array to the end of the array name, thus identifying it as an array. I also added a three-character string to the beginning of the array name in order to identify the type of data that array will hold. I recommend that you employ a similar naming scheme when you define your own arrays.

Loading Array Elements

Once you have declared your array, you can populate it with data, as demonstrated below.

 astrCustomerNamesArray(0) = "Markland B." astrCustomerNamesArray(1) = "Mike M." astrCustomerNamesArray(2) = "Nick C." astrCustomerNamesArray(3) = "Jerry F." astrCustomerNamesArray(4) = "Rocky B." 

Once populated, you can reference pieces of data stored in the array by specifying the name of the array followed by an index number, as demonstrated below.

 MessageBox.Show strCustomerNamesArray(2) 

Retrieving Array Elements

Because data stored in an array is usually related, you can set up a loop to process the array contents using just a few lines of code. This is a lot faster and more efficient than trying to specify each piece of data in the array one at a time using its index number. For example, suppose you have an array that contains 1,000 names. It wouldn't be practical to reference each item in the array individually. Instead, a better way to process the contents of the array would be to set up a ForNext loop.

The syntax of the For...Next loop is outlined below.

 For counter = begin To end    statements Next 

Counter represents a variable that will be used to control the execution of the loop. Begin is a number specifying the starting value of the counter variable, and End specifies the ending value for the counter variable. To see how to use the ForNext loop to process the contents of an array, take a look at the following example.

 Public Class Form1     Private Sub Form1_Load(ByVal sender As System.Object, _       ByVal e As System.EventArgs) Handles MyBase.Load         Dim strMessage As String = ""         Dim strCustomerNamesArray(4) As String         Dim intCounter As Integer         strCustomerNamesArray(0) = "Markland B."         strCustomerNamesArray(1) = "Mike M."         strCustomerNamesArray(2) = "Nick C."         strCustomerNamesArray(3) = "Jerry F."         strCustomerNamesArray(4) = "Rocky B."         For intCounter = 0 To 4             strMessage = strMessage & _               strCustomerNamesArray(intCounter) & _               ControlChars.NewLine         Next intCounter         MessageBox.Show(strMessage)     End Sub End Class 

As you can see in this example, the ForNext loop was used to spin through each element in the strCustomerNamesArray and put together a display string, which was displayed by the MessageBox.Show method once the loop had finished running.

You will find additional information on the For..Next loop in Chapter 7, "Processing Lots of Data with Loops."

Resizing Arrays

There may be times when you need to increase the size of an array to accommodate additional data. When you think this might be the case, you should declare the array without specifying its initial size, as demonstrated below.

 Dim strCustomerNamesArray() As String 

Then later, perhaps after you have queried the user as to how much information will be provided, you can resize the array using the ReDim keyword, as shown below.

 ReDim strCustomerNamesArray(9) 

At this point, you can begin populating the array with data. If, however, you find that you have not made the array big enough, you can always resize it again, as shown below.

 ReDim Preserve strCustomerNamesArray(19) 

Note that this time I added the keyword Preserve just before the array name. This keyword tells Visual Basic not to delete any data already stored in the array when resizing it. If you forget to add the Preserve keyword when resizing an array, you'll lose any already populated data.

Trap 

In addition to increasing the size of an array, you can make it smaller. But if you do so, you'll lose data if you resize the array smaller than the number of elements already stored in it.

Erasing Arrays

Once your program has finished using the data stored in an array, it's a good idea to delete the array, thus freeing up memory. You can do this using the Erase keyword and the following syntax.

 Erase ArrayName 

For example, to delete strCustomerNamesArray, you would add the following statement at the appropriate location within your program code:

 Erase strCustomerNamesArray 




Microsoft Visual Basic 2005 Express Edition Programming for the Absolute Beginner
Microsoft Visual Basic 2005 Express Edition Programming for the Absolute Beginner
ISBN: 1592008143
EAN: 2147483647
Year: 2006
Pages: 126

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