Unformatted text preview:

Chapter 8 – Arrays and StructuresSlide 2Slide 3Slide 4Slide 5Slide 6Slide 7Slide 8Slide 9Slide 10Slide 11Slide 12Slide 13Slide 14Slide 15Slide 16Slide 17Slide 18Slide 19Slide 20Slide 21Slide 22Slide 23Slide 24Slide 25Slide 26Slide 27Slide 28Slide 29Slide 30Slide 31Slide 32Slide 33Slide 34Slide 35Slide 36Slide 37Slide 38Slide 39Slide 40Slide 41Slide 42Slide 43Slide 44Slide 45Slide 46Slide 47Slide 48Slide 49Slide 50Slide 51Slide 52Slide 53Slide 54Slide 55Slide 56Slide 57Slide 58Slide 59Slide 60Slide 61Slide 62Slide 63Slide 64Slide 65Slide 66The Visual Basic .NET Coach1Chapter 8 – Arrays and StructuresControls like the MS flex grid expand on the concept of a programming construct called an array. An array allows you to store multiple values of a data type in a single variable. An MS flex grid allows you to do this and provides an interface for the user to see the results graphically. While arrays give you an extremely efficient method of storing data, their rigid structure makes it challenging to delete values or store values of different data types. Visual Basic .NET provides the collection object to allow you to store objects in a single construct of different data types with the ability to easily add, access, and delete values.The Visual Basic .NET Coach28.1 ArraysAn array is declared using the following syntax:Chapter 8 – Arrays and StructuresDim ArrayName(UpperBound) As DatatypeAn array’s name obeys the same rules as when declaring a variable: it begins with a letter and may be followed by any number of letters, underscores, or digits. An array name can be as small as one letter or as large as 255 letters, underscores, and digits.Individual values within an array are selected by using an index. The lowest index of an array is 0, while the upper bound of the array is the highest index in the array. An index must be a Short, an Integer, or a Long data type.The data type is any valid variable like a Short, an Integer, or a String.The Visual Basic .NET Coach3The following code defined an array of six Short values called shtGrades. With valid indexes from 0 to 5, you have six elements.Chapter 8 – Arrays and StructuresDim shtGrades(5) As ShortPictorially, the array would be represented as follows:Array shtGradesIndex 0 1 2 3 4 5Values Relating to Index 0 0 0 0 0 0The Visual Basic .NET Coach4To access an individual value in an array, you place the subscript of the value you wish to access inside the parentheses:Chapter 8 – Arrays and StructuresArrayName(Index)If you wanted to output the value stored in the array, shtGrades, at index 3, you could do so in a message box using the following code:Array shtGradesIndex 0 1 2 3 4 5Values Relating to Index 90 80 77 100 95 67MsgBox(shtGrades(3).ToString())To store grades into the array shtGrades, you would use the following syntax:shtGrades(0) = 90shtGrades(1) = 80shtGrades(2) = 77shtGrades(3) = 100shtGrades(4) = 95shtGrades(5) = 67Graphically, the array would appear as follows:The Visual Basic .NET Coach5Arrays of Other Data TypesArrays can be used to store values of other types of data besides numerical data. Arrays can be created from any data types. The following code shows how you can create an array of five Strings and initialize them to "Allen Iverson", "Aaron McKie", "Eric Snow", "Matt Harpring", and "Derrick Coleman".Chapter 8 – Arrays and StructuresDim strSixersNames(4) As StringstrSixersNames(0) = "Allen Iverson"strSixersNames(1) = "Aaron McKie"strSixersNames(2) = "Eric Snow"strSixersNames(3) = "Matt Harpring"strSixersNames(4) = "Derrick Coleman"The Visual Basic .NET Coach6Arrays of Other Data Types ContinuedIf you wanted to store the salary of each player in terms of millions of dollars, you could store them in an array of Single variables. If the salaries of each player were $7 million for Allen Iverson, $5.5 million for Aaron McKie, $4 million for Eric Snow, $2.7 million for Matt Harpring, and $8 million for Derrick Coleman, you could store them in the following code:Chapter 8 – Arrays and StructuresDim sngSixersSalaries(5) As SinglesngSixersSalaries(0) = 7sngSixersSalaries(1) = 5.5sngSixersSalaries(2) = 4sngSixersSalaries(3) = 2.7sngSixersSalaries(4) = 8The Visual Basic .NET Coach7Drill 8.1If an array has five elements, what is the highest index of a valid element in the array?Chapter 8 – Arrays and StructuresAnswer: 4The Visual Basic .NET Coach8Drill 8.2How many elements are defined in the following array declaration?Chapter 8 – Arrays and StructuresDim strDrillArray(5) As StringAnswer: 6The Visual Basic .NET Coach9Drill 8.3Write the code required to declare an array of five Strings called strDrillArray. Then initialize the array to the following values: "First Value", "Second Value", "Third Value", "Fourth Value", and "Fifth Value".Answer:Chapter 8 – Arrays and StructuresDim strDrillArray(4) As StringstrDrillArray(0) = "First Value"strDrillArray(1) = "Second Value"strDrillArray(2) = "Third Value"strDrillArray(3) = "Fourth Value"strDrillArray(4) = "Fifth Value"The Visual Basic .NET Coach10Drill 8.4Write the code required to declare an array of 1,000 Integers called intDrillArray with an index whose lower bound is 0 and upper bound is 999. Then initialize the array so that each element contains the value of its index. Hint: Use a loop.Answer:Chapter 8 – Arrays and StructuresDim intDrillArray(999) As IntegerDim intIndex As IntegerFor intIndex = 0 To 999intDrillArray(intIndex) = intIndexNext intIndexThe Visual Basic .NET Coach11Stepping Through an ArrayTo perform some action upon every element in the array you can use a For loop that steps from an index of 0 to the array’s upper bound.To output all of the values in the array strSixersNames use the following code:Chapter 8 – Arrays and StructuresDim shtSixerName As ShortFor shtSixerName = 0 To 4 MsgBox(strSixersNames(shtSixerName))NextThe code assumes that you know the upper bound of the array. It is convenient to use the UBound function to determine the upper bound of an array. By passing an array to UBound, the upper bound of the array is returned. The following shows the previous code rewritten using UBound to determine the highest value of the loop.Dim shtSixerName As ShortFor shtSixerName = 0 To Ubound(strSixersNames) MsgBox(strSixersNames(shtSixerName))NextThe Visual Basic .NET Coach12Stepping Through an Array ContinuedYou can loop through the array by stepping through each element of the array. Do not concern yourself with lower or upper


View Full Document

UCR CS 5 - Chapter 8 Arrays and Structure

Download Chapter 8 Arrays and Structure
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Chapter 8 Arrays and Structure and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Chapter 8 Arrays and Structure 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?