Arrays: Variables That Represent More Than One Value

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

As explained in 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 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 that is shown here.


Dim players() As String


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


Dim players(8) As String


The size of the array is 9 because a baseball team has 9 players. An array consists of a number of values, or elements, which are referenced by unique index values, starting with 0. The index value of the final element is always one less than the size of the array. In this case, the array contains the elements 0 through 8, for a total of nine elements. When you want to refer to one of the players on the team, you just subtract 1. For example, to reference the first player, you reference element 0, to reference the ninth player, you reference element 8.

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


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


In the previous code, the value John is assigned to the first element of the array (element 0) and the value Bart is assigned to the fourth element (element 3). 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, braces 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.

Just as you use numbers to specify an item's position in an array, you use the element number to specify the value that 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.

To store values in an array

  1. On the File menu, click New Project.

  2. In the New Project dialog box, in the Templates pane, click Windows Forms Application.

  3. In the Name box, type MyFirstArray and then click OK.

    A new Windows Forms project opens.

  4. From the Toolbox, drag a Textbox control onto the form.

  5. From the Toolbox, drag a Button control onto the form.

  6. Double-click the Button to open the Code Editor.

  7. In the Button1_Click event procedure, add the following code:

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

    Notice that the previous 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.

  8. Press F5 to run the program.

  9. 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.

    TipTip

    You should write additional code to check that the data entered is valid. For example, you can check that the value entered is a numeric value between 0 and 8. For more information, see What To Do When Something Goes Wrong: Handling Errors.

In this lesson, you learned how to use arrays to store and retrieve groups of similar values.

In the next lesson, you will learn how to use arithmetic operators to create expressions.

Next Lesson: Arithmetic: Creating Expressions with Variables and Operators

Show: