170 likes | 180 Views
Clearly Visual Basic: Programming with Visual Basic 2008. Chapter 19 A Ray of Sunshine. Objectives. Explain the purpose of an array Create a one-dimensional array Store data in a one-dimensional array Sort the contents of a one-dimensional array Search a one-dimensional array.
E N D
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 19 A Ray of Sunshine
Objectives • Explain the purpose of an array • Create a one-dimensional array • Store data in a one-dimensional array • Sort the contents of a one-dimensional array • Search a one-dimensional array Clearly Visual Basic: Programming with Visual Basic 2008
Let’s Join the Group • Simple variable (scalar variable) • One that is unrelated to any other variable in memory • Array of variables • Group of related variables • Each variable in an array • Has the same name and data type • Subscript • Unique number used to distinguish one variable in a one-dimensional array from another Clearly Visual Basic: Programming with Visual Basic 2008
Let’s Join the Group (continued) • Elements in a numeric array • Initialized to the number 0 • Elements in a String array • Initialized to the empty string • Populating the array • Assigning initial values to an array Clearly Visual Basic: Programming with Visual Basic 2008
My Friends Application • Will perform the following tasks: • Store four names in a one-dimensional String array named strFriends • Display the contents of the array in three label controls named: • lblOriginal, lblAscending, and lblDescending • To sort a one-dimensional array in descending order: • First, sort the values in ascending order; then use Array.Reversemethodto reverse array elements Clearly Visual Basic: Programming with Visual Basic 2008
Salary Application • Will store six salary amounts in a one-dimensional Integer array named intSalaries • Each salary amount corresponds to a salary code • The valid codes are the numbers 1 through 6 • Figure 19-9 • Shows planning information for the application • Before accessing an array element: • Procedure should verify that the subscript is valid Clearly Visual Basic: Programming with Visual Basic 2008
States Application • Stores the names of nine states in a one-dimensional String array named strStates • Names are stored in the order each was visited by the user • Interface provides a text box for the user to enter a state name • Figure 19-15 • Shows planning information for the application • Figure 19-16 • Shows Step Four of algorithm Clearly Visual Basic: Programming with Visual Basic 2008
Summary • Variables in an array • Have the same name and data type • Each element in a one-dimensional array • Identified by a unique subscript that appears in parentheses after the array’s name • When declaring a one-dimensional array: • Provide either the highest subscript or the initial values Clearly Visual Basic: Programming with Visual Basic 2008
Summary (continued) • Number of elements in a one-dimensional array • One number more than its highest subscript • Refer to an element in a one-dimensional array • Using the array’s name followed by the element’s subscript • You can use array variables just like any other variable Clearly Visual Basic: Programming with Visual Basic 2008
Summary (continued) • One-dimensional array’s Length property • Contains an integer that represents the number of elements in the array • Array.Sort method • Sorts elements in a one-dimensional array in ascending order • Array.Reverse method • Reverses the order of the elements in a one-dimensional array Clearly Visual Basic: Programming with Visual Basic 2008