One-Dimensional Arrays continued• You refer to each variable in an array by its name and the variable’s subscript Figure 10-3: Names of the variables in a one-dimensional array... Manipu
Trang 1Visual basic Net Arrays
Trang 2Using a One-Dimensional Array
Lesson A Objectives
• 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
Trang 3Using a One-Dimensional Array
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
Trang 4• A simple or scalar variable is one that is
unrelated to any other variable in memory
• An array is a group of variables that have the
same name and data type and are related in
some way
Trang 5Arrays (continued)
• The most commonly used arrays are
one-dimensional and two-one-dimensional
• Programmers use arrays to store related data in the internal memory of the computer
Trang 6One-Dimensional Arrays
• A one-dimensional array is simply a row (or
column) of variables
• Each element in an array is identified by a
subscript, which Visual Basic NET assigns to the variable when the array is created
Trang 7One-Dimensional Arrays (continued)
• You refer to each variable in an array by its name and the variable’s subscript
Figure 10-3: Names of the variables in a one-dimensional array
Trang 8One-Dimensional Arrays (continued)
• Declaring a one-dimensional array
Trang 9One-Dimensional Arrays (continued)
• Examples of declaring an array
– Dim cities(3) As String
– Private states() As String = {“Hawaii”, “Alaska”,
“Maine”}
Trang 10Storing Data in a One-Dimensional
Array
• In most cases, you use an assignment statement
to enter data into an existing array
• Syntax: arrayname(subscript) = value
• Examples
– cities(0) = “Madrid”
– cities(1) = “Paris”
– cities(2) = “Rome”
Trang 11Manipulating One-Dimensional
Arrays
• You will learn how to perform the following tasks using a one-dimensional array:
– Display the contents of an array
– Access an array element using its subscript
– Search the array
– Calculate the average of the data stored in a
numeric array
Trang 12Manipulating One-Dimensional
Arrays (continued)
• You will learn how to perform the following tasks using a one-dimensional array (continued):
– Find the highest value stored in an array
– Update the array elements
– Sort the array elements
Trang 13Displaying the Contents of a
One-Dimensional Array
• uiDisplayButton’s Click event procedure
– Demonstrates how you can display the contents of
an array in a label control
– Uses the For…Next statement to display each
array element
– You also could use the Do…Loop statement or the For Each…Next statement
Trang 14Displaying the Contents of a Dimensional Array (continued)
One-Figure 10-6: uiDisplayButton’s Click event procedure
Trang 15The For Each…Next Statement
Trang 16Using the Subscript to Access an Element in a One-Dimensional Array
• XYZ Corporation pays its managers based on six different salary codes, 1 through 6
• Each code corresponds to a different salary
• uiSalaryButton’s Click event procedure displays the salary corresponding to the code entered by the user
Trang 17Searching a One-Dimensional Array
• The sales manager at Jacobsen Motors wants a procedure that allows him to determine the
number of salespeople selling above a certain
amount, which he will enter
• uiSearchButton’s Click event procedure searches the array, looking for values that are greater than the amount entered by the sales manager
Trang 18Calculating the Average Amount Stored in a One-Dimensional
Numeric Array
• uiCalcAvgButton’s Click event procedure
calculates and displays the average test score
Figure 10-11: uiCalcAvgButton’s Click event procedure
Trang 19Calculating the Average Amount Stored in a One-Dimensional Numeric Array (continued)
Trang 20Determining the Highest Value
Stored in a One-Dimensional Array
• Sharon Johnson wants a procedure that displays the highest amount she has earned in a week
• uiHighestButton’s Click event procedure will
search the array, looking for the highest amount
Trang 21Updating the Values Stored in a
Trang 22Sorting the Data Stored in a
One-Dimensional Array
• Arranging data in a specific order is called sorting
• Array.Sort method
– Can be used to sort the elements in a
one-dimensional array in ascending order
– Syntax: Array.Sort(arrayname)
• uiSortButton’s Click event procedure uses the
Array.Sort method to sort the numbers array in ascending order
Trang 23Sorting the Data Stored in a Dimensional Array (continued)
One-• To sort a one-dimensional array in descending order:
– Use Array.Sort to sort the array in ascending order – Use Array.Reverse to reverse the array elements
• Syntax of the Array.Reverse method:
Array.Reverse(arrayname)
Trang 24Using a Module-Level
One-Dimensional Array
• Names application
– Needs to display the names contained in a
sequential access file
– Should give the user the choice of displaying the names in either ascending or descending order
• The names array is declared in the form’s
Declarations section, making it a module-level
array
Trang 25More on One-Dimensional Arrays
Lesson B Objectives
• Create and manipulate parallel one-dimensional arrays
• Create a structure
• Declare a structure variable
• Create and manipulate a one-dimensional array
of structures
Trang 26Parallel One-Dimensional Arrays
• Arrays that are related by an element’s position (subscript)
• Searching one array gives you the subscript for the other array
• To store a price list, which includes a string and a number, you can use two one-dimensional arrays
– A String array to store the product IDs
– An Integer array to store the prices
Trang 27Parallel One-Dimensional Arrays
(continued)
Figure 10-19: Illustration of a price list stored in two
Trang 28• Members included in the structure can be
variables, constants, or procedures
• In most cases, members are variables
Trang 29Structures (continued)
Trang 30Using a Structure to Declare a
Variable
• Variables declared using a structure are often
referred to as structure variables
Figure 10-22: Syntax and an example of declaring a structure
Trang 31Using a Structure to Declare a
Variable (continued)
Figure 10-23: Syntax
and examples of
Trang 32Creating an Array of Structure
Variables
• Assigning initial values to an array is referred to
as populating the array
• Refer to a member variable in an array element using the syntax:
arrayname(subscript).memberVariableName
Trang 33Using a Two-Dimensional Array
Lesson C Objectives
• Create and initialize a two-dimensional array
• Store data in a two-dimensional array
• Search a two-dimensional array
Trang 35Two-Dimensional Arrays (continued)
• Each variable (element) in a two-dimensional
array is identified by a unique combination of two subscripts
• The subscripts specify the variable’s row and
column position in the array
• Refer to each variable in a two-dimensional array
by the array’s name and the row and column
Trang 36Two-Dimensional Arrays (continued)
Figure 10-34: Syntax versions and examples of declaring a
Trang 37two-Storing Data in a Two-Dimensional
Array
Figure 10-35: Syntax
and examples of
Trang 38Searching a Two-Dimensional Array
Figure 10-36: uiDisplayPriceButton’s Click event procedure
Trang 39Searching a Two-Dimensional Array
Trang 40The Tax Calculator Application
• John Blackfeather, the owner and manager of the Perrytown Gift Shop, should be able to use the application to calculate the weekly federal
withholding tax for his employees
• To calculate the federal withholding tax, the user would need to enter the taxable wages in the
Taxable wages text box and then click the
Calculate Tax button
Trang 41The Tax Calculator Application
(continued)
Trang 42The Tax Calculator Application
(continued)
Figure 10-40: TOE chart for the Tax Calculator application
Trang 43Coding the uiCalculateButton Click
Event Procedure
Trang 44Coding the uiCalculateButton Click
Event Procedure (continued)
Figure 10-41: Pseudocode for the uiCalculateButton’s Click
Trang 46• To create an array of structures, use the
Structure statement to create a record structure, then use the record structure to declare the array