09

W
Shared by: yaoyufang
Categories
Tags
-
Stats
views:
2
posted:
9/16/2011
language:
English
pages:
62
Document Sample
scope of work template
							    Programming with
Microsoft Visual Basic 2008
      Fourth Edition

        Chapter Nine
          Arrays
 Previewing the Treasures Gift Shop
              Application
• Treasures application:
    – Allows the user to enter a product ID, then
      displays the product’s price
• Open the Treasures.exe file




Programming with Microsoft Visual Basic 2008, Fourth Edition   2
 Previewing the Treasures Gift Shop
       Application (continued)




            Figure 9-2: Interface showing the product’s price




Programming with Microsoft Visual Basic 2008, Fourth Edition    3
                 Lesson A Objectives

After studying Lesson A, you should be able to:
• Declare and initialize a one-dimensional array
• Store data in a one-dimensional array
• Display the contents of a one-dimensional array
• Code a loop using the For Each…Next statement
• Access an element in a one-dimensional array




Programming with Microsoft Visual Basic 2008, Fourth Edition   4
    Lesson A Objectives (continued)

• Search a one-dimensional array
• Compute the average of a one-dimensional
  array’s contents
• Find the highest entry in a one-dimensional array
• Update the contents of a one-dimensional array
• Sort a one-dimensional array




Programming with Microsoft Visual Basic 2008, Fourth Edition   5
                          Using Arrays
• Simple (scalar) variable:
    – One that is unrelated to any other variable in
      memory
• Array: Group of variables that have same name
  and same data type and are related in some way
• Reasons to use arrays:
    – Simplifies process of coding application
    – Increases run-time efficiency of program



Programming with Microsoft Visual Basic 2008, Fourth Edition   6
             One-Dimensional Arrays

• One-dimensional array:
    – Sequence of contiguous memory cells
    – Visualized as a column of variables
• Subscript: Integer identifying array variable
    – Starts at 0 for first array variable
• Refer to array variable by array name and
  subscript
    – Example: strCities(0) is first variable in states
      array

Programming with Microsoft Visual Basic 2008, Fourth Edition   7
  One-Dimensional Arrays (continued)




Figure 9-3: Names of the variables in the one-dimensional strCities array



  Programming with Microsoft Visual Basic 2008, Fourth Edition          8
One-Dimensional Arrays (continued)




Figure 9-4: Syntax versions and examples of declaring a one-dimensional
                                 array




Programming with Microsoft Visual Basic 2008, Fourth Edition              9
One-Dimensional Arrays (continued)




      Figure 9-4: Syntax versions and examples of declaring a one-
                      dimensional array (continued)
Programming with Microsoft Visual Basic 2008, Fourth Edition         10
One-Dimensional Arrays (continued)

• Element: Refers to individual array variable
• Arrays are initialized by computer when created:
    – Arrays of numeric variables are initialized to 0
    – Arrays of string variables are initialized using
      keyword Nothing
    – Arrays of Boolean variables are initialized to False
    – Arrays of Date variables are initialized to 12:00
      AM January 1, 0001
• Populating the array: Assigning initial values

Programming with Microsoft Visual Basic 2008, Fourth Edition   11
One-Dimensional Arrays (continued)

• After array is declared, you can store data in
  array
• To enter data into array:
   • Use assignment statement
   • Use TryParse statement
• Syntax: arrayname(subscript) = value




Programming with Microsoft Visual Basic 2008, Fourth Edition   12
One-Dimensional Arrays (continued)




     Figure 9-5: Examples of statements used to store data in a one-
                           dimensional array
Programming with Microsoft Visual Basic 2008, Fourth Edition           13
        Manipulating One-Dimensional
                    Arrays
• Tasks to be performed with one-dimensional
  array:
    –   Display contents of array
    –   Access array element using its subscript
    –   Search array
    –   Calculate average of data stored in numeric array
    –   Find highest value stored in array
    –   Update array elements
    –   Sort array elements

Programming with Microsoft Visual Basic 2008, Fourth Edition   14
            Displaying the Contents
           of a One-Dimensional Array
• Months application: Stores names of 12 months
  of year and displays them in list box
• The form’s Load event procedure:
    – Declares array named strMonths, with initial
      values
    – Array values are transferred to list box using
      For…Next loop
    – First item in list box is selected as default display
      value


Programming with Microsoft Visual Basic 2008, Fourth Edition   15
     Displaying the Contents of a
   One-Dimensional Array (continued)




         Figure 9-6: Result of starting the Months application

Programming with Microsoft Visual Basic 2008, Fourth Edition     16
     The For Each…Next Statement

• For Each…Next statement: Used to process
  each element in array
• Unlike For…Next statement:
    – You do not have to code starting and ending
      subscripts
    – You do not have to use extra array notation
• Declare variable within For Each…Next
  statement to refer to each array element, one at
  a time

Programming with Microsoft Visual Basic 2008, Fourth Edition   17
     The For Each…Next Statement
               (continued)




    Figure 9-7: Syntax and examples of the For Each…Next statement

Programming with Microsoft Visual Basic 2008, Fourth Edition         18
   Using the Subscript to Access an
  Element in a One-Dimensional Array

• Salary code application:
    – Uses one-dimensional array to store six salary
      amounts
• Each salary amount is associated with code
• Salary code is entered by user
• Salary amount is displayed with button click




Programming with Microsoft Visual Basic 2008, Fourth Edition   19
   Using the Subscript to Access an
  Element in a One-Dimensional Array
              (continued)




                   Figure 9-8: Codes and salary amounts


Programming with Microsoft Visual Basic 2008, Fourth Edition   20
   Using the Subscript to Access an
  Element in a One-Dimensional Array
              (continued)




       Figure 9-9: Salary amount shown in the Salary amount box

Programming with Microsoft Visual Basic 2008, Fourth Edition      21
 Determining the Length of an Array

• Subscript used to access array element must be
  at least 0 but no more than highest subscript in
  array
    – Otherwise runtime error will occur
• Length property: Stores number of elements in
  array




Programming with Microsoft Visual Basic 2008, Fourth Edition   22
 Determining the Length of an Array
             (continued)




    Figure 9-10: Result of the runtime error caused by an invalid subscript


Programming with Microsoft Visual Basic 2008, Fourth Edition             23
 Determining the Length of an Array
             (continued)




       Figure 9-11: Modified SelectedValueChanged procedure




Programming with Microsoft Visual Basic 2008, Fourth Edition   24
Searching a One-Dimensional Array

• Sales Solution application:
    – Sales manager enters sales amount
    – Sales manager presses Search button
    – Number of salespeople selling more than specified
      amount are displayed




Programming with Microsoft Visual Basic 2008, Fourth Edition   25
Searching a One-Dimensional Array
            (continued)




           Figure 9-12: btnSearch control’s Click event procedure
Programming with Microsoft Visual Basic 2008, Fourth Edition        26
Searching a One-Dimensional Array
            (continued)




Figure 9-13: Interface showing the number of salespeople selling over $4000




Programming with Microsoft Visual Basic 2008, Fourth Edition          27
    Calculating the Average Amount
      Stored in a One-Dimensional
              Numeric Array
• Average application:
    – Calculates and displays average test scores
    – Uses four-element one-dimensional Integer array
• btnCalc control’s Click event procedure:
    – Adds array element values
    – Divides total by number of array elements
    – Displays average test score



Programming with Microsoft Visual Basic 2008, Fourth Edition   28
    Calculating the Average Amount
      Stored in a One-Dimensional
       Numeric Array (continued)




    Figure 9-14: Code for the btnCalc control’s Click event procedure
Programming with Microsoft Visual Basic 2008, Fourth Edition            29
    Determining the Highest Value
   Stored in a One-Dimensional Array
• Highest Number application:
    – Displays highest number stored in 10-element
      one-dimensional Double array
• btnDisplay control’s Click event procedure:
    – Searches array, looking for highest amount
    – Displays highest amount




Programming with Microsoft Visual Basic 2008, Fourth Edition   30
    Determining the Highest Value
   Stored in a One-Dimensional Array
               (continued)




           Figure 9-16: btnDisplay control’s Click event procedure
Programming with Microsoft Visual Basic 2008, Fourth Edition         31
     Updating the Values Stored in a
         One-Dimensional Array
• Price Increase application:
    – Stores prices of four items in four-element, one-
      dimensional Decimal array
    – Used to raise price of each item the company sells
    – Displays each item’s new price in label control
• btnDisplay control’s Click event procedure:
    –   Stores original prices in array
    –   Retrieves amount of increase input to text box
    –   Adds amount of increase to each array element
    –   Displays each new value stored in array
Programming with Microsoft Visual Basic 2008, Fourth Edition   32
    Updating the Values Stored in a
   One-Dimensional Array (continued)




        Figure 9-18: Click event procedure for the btnDisplay control
Programming with Microsoft Visual Basic 2008, Fourth Edition            33
    Updating the Values Stored in a
   One-Dimensional Array (continued)




             Figure 9-19: Interface showing the old and new prices


Programming with Microsoft Visual Basic 2008, Fourth Edition         34
             Sorting the Data Stored
           in a One-Dimensional Array
• Sorting: Arranging data in specific order
• Array.Sort method:
    – Sorts elements of one-dimensional array in
      ascending order
    – Syntax: Array.Sort(arrayname)
• To sort array in descending order:
    – First use Array.Sort to sort in ascending order
    – Then use Array.Reverse to reverse order of array
      elements

Programming with Microsoft Visual Basic 2008, Fourth Edition   35
     Sorting the Data Stored in a
   One-Dimensional Array (continued)




    Figure 9-21: Interface showing the state names in ascending order



Programming with Microsoft Visual Basic 2008, Fourth Edition            36
                  Lesson A Summary

• An array groups variables with same data type
  under one name
• An individual array variable is also called an
  element
• Arrays may be declared with or without list of
  initial values
• Values can be assigned to array after declaration




Programming with Microsoft Visual Basic 2008, Fourth Edition   37
     Lesson A Summary (continued)

• Use For Each…Next statement to process
  instructions for each element in array
• To refer to array element, use array’s name
  followed by element’s subscript
• Length property of array returns array size
• Array.Sort: Sorts elements in ascending order
• Array.Reverse: Reverses order of array elements




Programming with Microsoft Visual Basic 2008, Fourth Edition   38
                 Lesson B Objectives
After studying Lesson B, you should be able to:
• Create and manipulate parallel one-dimensional
  arrays
• Locate information in two parallel one-
  dimensional arrays
• Declare and initialize a two-dimensional array
• Store data in a two-dimensional array
• Search a two-dimensional array
• Determine the highest subscript in a two-
  dimensional array
Programming with Microsoft Visual Basic 2008, Fourth Edition   39
    Parallel One-Dimensional Arrays
• Parallel one-dimensional arrays:
    – Two or more arrays whose elements are related
      by their position in arrays (by their subscripts)
    – Arrays may be different data types
• Scenario involving two parallel arrays:
    – Parallel arrays are named strIds and intPrices
    – Each strIds element corresponds to intPrices
      element
    – Must search strIds array for item ID
    – If ID exists, access price at same index in intPrices
      array
Programming with Microsoft Visual Basic 2008, Fourth Edition   40
    Parallel One-Dimensional Arrays
               (continued)




         Figure 9-23: Illustration of a price list stored in two parallel
                            one-dimensional arrays

Programming with Microsoft Visual Basic 2008, Fourth Edition                41
    Parallel One-Dimensional Arrays
               (continued)




       Figure 9-24: btnDisplay control’s Click event procedure using
                      parallel one-dimensional arrays
Programming with Microsoft Visual Basic 2008, Fourth Edition           42
    Parallel One-Dimensional Arrays
               (continued)




          Figure 9-24: btnDisplay control’s Click event procedure using
                   parallel one-dimensional arrays (continued)


Programming with Microsoft Visual Basic 2008, Fourth Edition              43
    Parallel One-Dimensional Arrays
               (continued)




       Figure 9-25: Interface showing the price for product ID CR20


Programming with Microsoft Visual Basic 2008, Fourth Edition          44
             Two-Dimensional Arrays

• Two-dimensional array:
    – Stores variables (elements) in rows and columns
    – Resembles a table
• How to identify a two-dimensional array element:
    – Use unique combination of two subscripts to
      specify element’s row and column position
         • Example: strProducts(1,2) refers to row two,
           column three




Programming with Microsoft Visual Basic 2008, Fourth Edition   45
Two-Dimensional Arrays (continued)




    Figure 9-26: Names of some of the elements in the strProducts array


Programming with Microsoft Visual Basic 2008, Fourth Edition          46
Two-Dimensional Arrays (continued)




Figure 9-27: Syntax and examples of declaring a two-dimensional array
Programming with Microsoft Visual Basic 2008, Fourth Edition            47
Two-Dimensional Arrays (continued)




         Figure 9-28: Examples of statements used to store data in a
                          two-dimensional array

Programming with Microsoft Visual Basic 2008, Fourth Edition           48
Searching a Two-Dimensional Array

• Two-dimensional arrays versus parallel arrays:
    – Both can represent data in tabular format
    – All data in two-dimensional array must be same
      type
• New version of Treasures application:
    – Use one two-dimensional array to store price list
    – 2-D array replaces two parallel arrays in first
      version



Programming with Microsoft Visual Basic 2008, Fourth Edition   49
Searching a Two-Dimensional Array
            (continued)




    Figure 9-29: Two-dimensional array declaration statement entered in
                             the procedure




Programming with Microsoft Visual Basic 2008, Fourth Edition          50
        The GetUpperBound Method

• GetUpperBound method:
    – Returns integer indicating highest subscript in
      specified dimension of array
• Dimension will be 0 for row dimension, and 1 for
  column dimension




Programming with Microsoft Visual Basic 2008, Fourth Edition   51
        The GetUpperBound Method
                (continued)




   Figure 9-30: Syntax and examples of the GetUpperBound method
Programming with Microsoft Visual Basic 2008, Fourth Edition      52
        The GetUpperBound Method
                (continued)




      Figure 9-31: btnDisplay control’s Click event procedure using a
                          two-dimensional array
Programming with Microsoft Visual Basic 2008, Fourth Edition            53
                  Lesson B Summary
• The elements in parallel one-dimensional arrays
  are related by their subscript position
• To create parallel one-dimensional arrays, create
  two one-dimensional arrays with same size
• A two-dimensional array contains rows and
  columns
• Use row and column subscripts to refer to
  element in two-dimensional array
• Use GetUpperBound method to determine
  highest subscript in specified dimension of an
  array
Programming with Microsoft Visual Basic 2008, Fourth Edition   54
                 Lesson C Objectives

After studying Lesson C, you should be able to:
• Create a simple smart device application




Programming with Microsoft Visual Basic 2008, Fourth Edition   55
Creating a Smart Device Application

• Smart Device applications: Run on smart devices
  such as Pocket PCs and Smartphones
• Visual Studio provides emulators of devices to
  use for testing application
• In Visual Studio, select Smart Device Project
  template




Programming with Microsoft Visual Basic 2008, Fourth Edition   56
Creating a Smart Device Application
            (continued)




                  Figure 9-37: New Project dialog box
Programming with Microsoft Visual Basic 2008, Fourth Edition   57
Creating a Smart Device Application
            (continued)




                   Figure 9-39: Pocket PC emulator form
Programming with Microsoft Visual Basic 2008, Fourth Edition   58
Creating a Smart Device Application
            (continued)




               Figure 9-40: Controls added to the emulator form
Programming with Microsoft Visual Basic 2008, Fourth Edition      59
Creating a Smart Device Application
            (continued)




              Figure 9-42: Code you need to enter in the btnDisplay
                         control’s Click event procedure
Programming with Microsoft Visual Basic 2008, Fourth Edition          60
Creating a Smart Device Application
            (continued)




                 Figure 9-44: Price displayed on the emulator device

Programming with Microsoft Visual Basic 2008, Fourth Edition           61
                  Lesson C Summary

• To run an application on a Smart Device, use
  Smart Device project template




Programming with Microsoft Visual Basic 2008, Fourth Edition   62

						
Related docs
Other docs by yaoyufang
FAQs Contactors
Views: 22  |  Downloads: 0
The DIRECTV Group_ Inc
Views: 328  |  Downloads: 1
GM Korea’s Roadside Assistance
Views: 5  |  Downloads: 0
REGULAR COUNCIL MEETING A G E N D A
Views: 1  |  Downloads: 0
Music Listening Today Chapter 27-28 Questions
Views: 4  |  Downloads: 0
CORPUS CHRISTI RECTIFIER SEMINAR (DOC)
Views: 8  |  Downloads: 0
801
Views: 8  |  Downloads: 0
Spring Fling Basket Themes
Views: 10  |  Downloads: 0
Northern Arizona Behavioral Health Authority
Views: 2  |  Downloads: 0