Organizing Data with Arrays

Simply stated, an array is a group of one or more data items that share a common name . In previous chapters, we have used a data definition similar to the following statements:

 Dim DayOfWeek as String  DayOfWeek = "Monday" 

As you know from earlier chapters, after processing the Dim statement, Visual Basic .NET creates an entry in its symbol table and allocates space in memory for a String variable named DayOfWeek . The second program statement assigns the string literal "Monday" into the variable named DayOfWeek . This works fine as it is, but what if you want to have variables for each day of the week?

In that case, you could do something like the following:

 Dim Day0 As String, Day1 As String, Day2 As String, Day3 As String  Dim Day4 As String, Day5 As String, Day6 As String Day0 = "Monday" Day1 = "Tuesday" Day2 = "Wednesday"         ' We'll skip the obvious statements that follow... 

You get the idea. This code works fine, but it is not very convenient to use in a program. Obviously, what you want in this case is a list of the days of the week. The code here forces you to reference the seven days of the week as seven different variables. Clearly, the days of the week are conceptually related to each other, and that's the way you would like to reference them: as a related list. That is exactly what arrays allow you to do.

You can simplify the day-of-the-week code as follows :

 Dim DaysOfTheWeek(6) As String  DaysOfTheWeek (0) = "Monday" DaysOfTheWeek (1) = "Tuesday" DaysOfTheWeek (2) = "Wednesday"             ' ...and so on... 

The first thing to notice is that you've decreased the number of data definitions from seven to one. Now you have a single data definition of a string array named DaysOfTheWeek(6) . What distinguishes this data definition from the previous String definitions is the (6) after the variable name. The purpose of (6) is to set the number of elements in the DaysOfTheWeek string array.

Array Elements

Elements? What is an array element? When you define any type of array, you are actually defining a set of variables of the same data type that can be referenced by a single name. Each one of those variables is called an element of the array.

So how many elements have you defined for the DaysOfTheWeek() array? By default, all arrays in Visual Basic .NET begin with element 0, not element 1. Therefore, this statement:

 Dim DaysOfTheWeek(6) As String 

defines array elements DaysOfTheWeek(0) through DaysOfTheWeek(6) . This means that you have actually defined seven elements for the string array named DaysOfTheWeek() .

When Visual Basic .NET sees this statement, it pretty much does the same thing it does with any other Dim statement. First, it checks to see whether you already have a variable named DaysOfTheWeek defined anywhere in the program. If you don't, Visual Basic .NET asks the Windows operating system for enough memory to hold a string array with seven array elements in it. If there is enough free memory for a string array with seven elements, Windows sends a message to Visual Basic .NET, saying everything's okay, and returns an lvalue (that is, a memory address) of where to put the array in memory.

Now let's review the next three statements:

 DaysOfTheWeek (0) = "Monday"  DaysOfTheWeek (1) = "Tuesday" DaysOfTheWeek (2) = "Wednesday" 

You already know that the equal sign in such a program statement performs an assignment of the expression on the right of the equal sign into the variable on the left side of the expression.

So what is the purpose of the values in parentheses in each of these expressions? The next section explains.

Array Indexes

To distinguish one element of an array from the others, each element of an array is associated with an index number. That index number is used to reference each distinct element of the array. Therefore, the first program line assigns "Monday" into the first element of the DaysOfTheWeek() array, the second line assigns "Tuesday" to the second element of the array, and so on.

Programmer's Tip


Many programmers use the word subscript to refer to the index number of an array. The two terms may be used interchangeably.

If you think about the Dim statement for the array, you should now understand the purpose of the 6 in the statement: The value in the parentheses in the Dim statement sets the upper limit of a valid index number for any element in that array. If you tried to access element DaysOfTheWeek(7) of the array, Visual Basic .NET would get upset because it only got enough memory from Windows to access up through element DaysOfTheWeek(6) of the array. If you attempted to access an element larger than the upper limit that was defined for the array, you would get an "Index was outside the bounds of the array" error message. You would also get that error message if you used a negative index number for an array element.

A Simple Program Example Using a String Array

To test your new knowledge of arrays, start a new Visual Basic .NET project and name it StringArray01 . Place the controls shown in Figure 7.1 on the form, using the names indicated for the text boxes. The button used to show the day of the week should be named btnShowDay and the Exit button should be named btnExit .

Figure 7.1. Components used in the StringArray01 project.


Add the following code to the btnExit object's Click() event:


This code line for the btnExit button should be fairly familiar to you by now. For this reason, this line is not mentioned in subsequent programs unless you need to add some additional code to the btnExit object's Click() event.

Now add the following code to the btnShowDay object's Click() event:

 Dim DaysOfTheWeek(6) As String  DaysOfTheWeek(0) = "Monday" DaysOfTheWeek(1) = "Tuesday" DaysOfTheWeek(2) = "Wednesday" DaysOfTheWeek(3) = "Thursday" DaysOfTheWeek(4) = "Friday" DaysOfTheWeek(5) = "Saturday" DaysOfTheWeek(6) = "Sunday" txtDay.Text = DaysOfTheWeek(CInt(txtIndex.Text)) 

This code should look familiar and probably doesn't need much explanation. The Dim statement defines a string array named DaysOfTheWeek() that has seven elements. The next seven lines simply initialize each element of the array with the appropriate string literal for the day, as determined by the index number for each array element.

The last program line converts the number entered by the user in the txtIndex text box into an integer by using the CInt() conversion routine, and it assigns that element of the DaysOfTheWeek() array into the txtDay text box. For example, if the user enters the number 3 into the txtIndex text box, the assignment statement is processed as follows:

 txtDay.Text = DaysOfTheWeek(CInt(txtIndex.Text))  txtDay.Text = DaysOfTheWeek(CInt("3")) ' "3" is still a string txtDay.Text = DaysOfTheWeek(3)     ' Now it's a numeric index txtDay.Text = "Thursday" 

The user would then see Thursday displayed in the txtDay text box.

Program Refinements

The StringArray01 program works fine as it is, but it has a confusing user interface. (The term user interface refers to the screen the user is expected to use to interact with the program.) The user interface is a little confusing because it shows two text boxes on the form as the program executes, but it expects the user to fill in only the first one. The second text box is used to display the output of the program.

The problem is that users are accustomed to filling in a text box when they see one on a program form. You really don't want the user to type anything into the txtDay text box. To fix this problem, make the following changes to the program:

  1. Click the label that contains the caption The Day is: . (This is probably called Label2 in your program.)

  2. Go to the Properties window. Click the Visible property and set its value to False . You can see the Visible property in the lower-right part of Figure 7.2. You can also see the drop-down list box that shows the value False highlighted.

    Figure 7.2. Setting the Visible property for Label2 to False .


  3. Click the txtDay text box and set its Visible property to False. You have now made the Label2 label and the txtDay text box invisible when the program begins execution.

  4. Make the following new lines the last two lines in the btnShowDay object's Click() event:

     Label2.Visible = True  txtDay.Visible = True 
  5. Compile and run the program. When the program begins execution, it should look as shown in Figure 7.3.

    Figure 7.3. StringArray01 program execution after you modify the Visible property.


Now the user can only type into the txtIndex text box, which is exactly what you want the user to do. After the user types a value into the txtIndex text box and clicks the btnShowDay button, the last two lines of the program make the second label and the txtDay text box visible by changing their Visible properties to True . The program output would then look like that shown in Figure 7.1, but with the proper day of the week shown in the txtDay text box.

Although the program is now a little more complex than it was before, the user can now more easily interact with the program. It is almost always a good idea to make things as simple as possible for the user, even if it means you must add new code to a program.

Visual Basic .NET. Primer Plus
Visual Basic .NET Primer Plus
ISBN: 0672324857
EAN: 2147483647
Year: 2003
Pages: 238
Authors: Jack Purdum

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: