620 likes | 915 Views
Previewing the Treasures Gift Shop Application. Treasures application:Allows the user to enter a product ID, then displays the product's priceOpen the Treasures.exe file . Programming with Microsoft Visual Basic 2008, Fourth Edition. 2. Previewing the Treasures Gift Shop Application (continued).
E N D
1. Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Nine
Arrays
2. 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
3. Previewing the Treasures Gift Shop Application (continued) Programming with Microsoft Visual Basic 2008, Fourth Edition 3
4. 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
5. 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
6. 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
7. 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
8. One-Dimensional Arrays (continued) Programming with Microsoft Visual Basic 2008, Fourth Edition 8
9. One-Dimensional Arrays (continued) Programming with Microsoft Visual Basic 2008, Fourth Edition 9
10. One-Dimensional Arrays (continued) Programming with Microsoft Visual Basic 2008, Fourth Edition 10
11. 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
12. 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
13. One-Dimensional Arrays (continued) Programming with Microsoft Visual Basic 2008, Fourth Edition 13
14. 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
15. 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
16. Displaying the Contents of a One-Dimensional Array (continued) Programming with Microsoft Visual Basic 2008, Fourth Edition 16
17. 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
18. Programming with Microsoft Visual Basic 2008, Fourth Edition 18 The For Each…Next Statement (continued)
19. 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
20. Using the Subscript to Access an Element in a One-Dimensional Array (continued) Programming with Microsoft Visual Basic 2008, Fourth Edition 20
21. Using the Subscript to Access an Element in a One-Dimensional Array (continued) Programming with Microsoft Visual Basic 2008, Fourth Edition 21
22. 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
23. Determining the Length of an Array (continued) Programming with Microsoft Visual Basic 2008, Fourth Edition 23
24. Determining the Length of an Array (continued) Programming with Microsoft Visual Basic 2008, Fourth Edition 24
25. 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
26. Searching a One-Dimensional Array (continued) Programming with Microsoft Visual Basic 2008, Fourth Edition 26
27. Searching a One-Dimensional Array (continued) Programming with Microsoft Visual Basic 2008, Fourth Edition 27
28. 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
29. Calculating the Average Amount Stored in a One-Dimensional Numeric Array (continued) Programming with Microsoft Visual Basic 2008, Fourth Edition 29
30. 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
31. Programming with Microsoft Visual Basic 2008, Fourth Edition 31 Determining the Highest Value Stored in a One-Dimensional Array (continued)
32. 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
33. Updating the Values Stored in a One-Dimensional Array (continued) Programming with Microsoft Visual Basic 2008, Fourth Edition 33
34. Updating the Values Stored in a One-Dimensional Array (continued) Programming with Microsoft Visual Basic 2008, Fourth Edition 34
35. 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
36. Sorting the Data Stored in a One-Dimensional Array (continued) Programming with Microsoft Visual Basic 2008, Fourth Edition 36
37. 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
38. 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
39. 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
40. 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
41. Parallel One-Dimensional Arrays (continued) Programming with Microsoft Visual Basic 2008, Fourth Edition 41
42. Parallel One-Dimensional Arrays (continued) Programming with Microsoft Visual Basic 2008, Fourth Edition 42
43. Parallel One-Dimensional Arrays (continued) Programming with Microsoft Visual Basic 2008, Fourth Edition 43
44. Parallel One-Dimensional Arrays (continued) Programming with Microsoft Visual Basic 2008, Fourth Edition 44
45. 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
46. Two-Dimensional Arrays (continued) Programming with Microsoft Visual Basic 2008, Fourth Edition 46
47. Two-Dimensional Arrays (continued) Programming with Microsoft Visual Basic 2008, Fourth Edition 47
48. Two-Dimensional Arrays (continued) Programming with Microsoft Visual Basic 2008, Fourth Edition 48
49. 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
50. Searching a Two-Dimensional Array (continued) Programming with Microsoft Visual Basic 2008, Fourth Edition 50
51. 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
52. Programming with Microsoft Visual Basic 2008, Fourth Edition 52 The GetUpperBound Method (continued)
53. Programming with Microsoft Visual Basic 2008, Fourth Edition 53 The GetUpperBound Method (continued)
54. 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
55. 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
56. 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
57. Creating a Smart Device Application (continued) Programming with Microsoft Visual Basic 2008, Fourth Edition 57
58. Programming with Microsoft Visual Basic 2008, Fourth Edition 58 Creating a Smart Device Application (continued)
59. Creating a Smart Device Application (continued) Programming with Microsoft Visual Basic 2008, Fourth Edition 59
60. Creating a Smart Device Application (continued) Programming with Microsoft Visual Basic 2008, Fourth Edition 60
61. Creating a Smart Device Application (continued) Programming with Microsoft Visual Basic 2008, Fourth Edition 61
62. 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