Arrays: Variables That Represent More Than One Value

来源:互联网 发布:nginx ssl模块加载 编辑:程序博客网 时间:2024/04/29 03:22

In this lesson, you will learn how to use arrays to store groups of values.

As you learned in the previous lessons, variables are used to store different types of data for use by your program. There is another type of variable called an array that provides a convenient way to store several values of the same type.

For example, suppose you were writing a program for a baseball team and you wanted to store the names of all of the players on the field. You could create nine separate string variables, one for each player, or you could declare an array variable that looks something like the code below.

Dim players() As String

You declare an array variable by putting parentheses after the variable name. If you know how many values you need to store, you can also specify the size of the array in the declaration as follows.

Dim players(8) As String

You may find it odd that the size of the array is 8 when a baseball team has 9 players. This is because an array is made up of a number of values or elements, starting with element 0 and ending with the number specified in the declaration. In this case, the array contains the elements 0 through 8, for a total of nine elements.
Assigning Values to Arrays

As with other types of values, you need to assign values to arrays. To do so, you refer to the element number as part of the assignment, as shown below.

players(0) = "John"
players(3) = "Bart"

In the above code, the value John is assigned to the first element of the array (element 0) and the value Brett is assigned to the fourth element (element3). The elements of the array don't have to be assigned in order, and any unassigned element will have a default value—in this case, an empty string.

As with other types of values, you can declare and assign values to an array on a single line as follows.

Dim players() As Integer = {1, 2, 3, 4, 5, 6, 7, 8, 9}

In this case, curly brackets indicate a list of values. The values are assigned to elements in the order listed. Notice that size of the array isn't specified—it is determined by the number of items that you list.
Retrieving Values from Arrays

Just as you use numbers to specify an item's position in an array, you use the element number to specify which value you want to retrieve.

Dim AtBat As String
AtBat = players(3)

The above code retrieves the fourth element of the array and assigns it to the string variable AtBat.
Try It!

To store values in an array
On the File menu, choose New Project.
In the New Project dialog box, in the Templates pane, click Windows Application.
 
In the Name box, type MyFirstArray and then click OK.
A new Windows Forms project opens.
 
From the Toolbox, drag a Textbox control onto the form.
 
From the Toolbox, drag a Button control onto the form.
 
Double-click the Button to open the Code Editor.
 
In the Button1_Click event procedure, add the following code:

Dim players() As String = {"Dan", "Fred", "Bart", "Carlos", _
"Ty", "Juan", "Jay", "Sam", "Pedro"}
Dim i As Integer = CInt(Textbox1.Text)
MsgBox(players(i) & " is on first base.")

Notice that the above code uses the CInt function to convert the String value (TextBox1.Text) to an Integer (i). You can learn more about conversions in Closer Look: Converting from One Variable Type to Another.
 
Press F5 to run the program.
 
Type a number between 0 and 8 in the text box and click the button. The name corresponding to that element is displayed in a message box. 

原创粉丝点击