Chapter 08
-
Upload
terry-yoast -
Category
Education
-
view
584 -
download
8
Transcript of Chapter 08
![Page 1: Chapter 08](https://reader036.fdocuments.us/reader036/viewer/2022062405/5560e877d8b42a3d768b505f/html5/thumbnails/1.jpg)
8-1
aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf
![Page 2: Chapter 08](https://reader036.fdocuments.us/reader036/viewer/2022062405/5560e877d8b42a3d768b505f/html5/thumbnails/2.jpg)
Arrays
Chapter 88
McGraw-Hill © 2006 The McGraw-Hill Companies, Inc. All rights reserved.
![Page 3: Chapter 08](https://reader036.fdocuments.us/reader036/viewer/2022062405/5560e877d8b42a3d768b505f/html5/thumbnails/3.jpg)
8-3
Objectives
Establish an array and refer to individual elements in the array with subscripts
Use the For Each/Next to traverse the elements of an array
Create a structure for multiple fields of related dataAccumulate totals using arraysDistinguish between direct access and indirect access of a
tableWrite a table lookup for matching an array elementCombine the advantages of list box controls with arraysStore and look up data in multidimensional arrays
![Page 4: Chapter 08](https://reader036.fdocuments.us/reader036/viewer/2022062405/5560e877d8b42a3d768b505f/html5/thumbnails/4.jpg)
8-4
Single-Dimension Arrays
List or series of values all referenced by the same nameSimilar to list of values for list boxes and combo boxes -
without the boxUse an array to store a series of variables for later
processingUse an array to store multiple valuesMay be referred to as a table or subscripted (index)
variableIndividual elements are treated the same as any other
variable and my be used in any statement
![Page 5: Chapter 08](https://reader036.fdocuments.us/reader036/viewer/2022062405/5560e877d8b42a3d768b505f/html5/thumbnails/5.jpg)
8-5
Array Terms
ElementIndividual item in the array
Subscript (or index)Zero based number used to reference the specific
elements in the arrayMust be an integer
BoundariesLower Subscript, 0 by defaultUpper Subscript
![Page 6: Chapter 08](https://reader036.fdocuments.us/reader036/viewer/2022062405/5560e877d8b42a3d768b505f/html5/thumbnails/6.jpg)
8-6
Array Example
nameString Array(0)(1)(2)(3)(4)(5)(6)(7)(8)(9)
Janet BakerGeorge LeeSue LiSamuel HoosierSandra WeeksWilliam MacyAndy HarrisonKen FordDenny FranksShawn James
![Page 7: Chapter 08](https://reader036.fdocuments.us/reader036/viewer/2022062405/5560e877d8b42a3d768b505f/html5/thumbnails/7.jpg)
8-7
Subscripts
Subscripts may be constants, variables, or numeric expressionsSubscripts must be integers-VB rounds any noninteger
subscript
![Page 8: Chapter 08](https://reader036.fdocuments.us/reader036/viewer/2022062405/5560e877d8b42a3d768b505f/html5/thumbnails/8.jpg)
8-8
The Declaration Statements for Arrays - General Form
This Dim statement allocates storage for specific number of elements and initializes numeric variables to 0 and string array elements to empty string (zero characters)
Elements in an array may be assigned values in the Dim statement, cannot declare upper subscript and initial values
Dim ArrayName(UpperSubscript) As DatatypeDim ArrayName( ) As Datatype = {InitialValueList}Dim ArrayName As Datatype( ) = {InitialValueList}
![Page 9: Chapter 08](https://reader036.fdocuments.us/reader036/viewer/2022062405/5560e877d8b42a3d768b505f/html5/thumbnails/9.jpg)
8-9
Dim Statement for Arrays Example(s)
Dim nameString(25) As StringDim balanceDecimal(10) As DecimalDim productString(99) As StringDim indexInteger( ) As Integer = {1, 5, 12, 18, 20}Dim indexInteger As Integer( ) = {1, 5, 12, 18, 20}Dim departmentsString( ) As String = {"Accounting", "Marketing"}Private categoryString(10) As StringPublic idNumberString(5) As String
![Page 10: Chapter 08](https://reader036.fdocuments.us/reader036/viewer/2022062405/5560e877d8b42a3d768b505f/html5/thumbnails/10.jpg)
8-10
Valid Subscripts
Subscript must reference a valid element of an arrayVB rounds fractional subscriptsVB throws exceptions for subscripts that are out of
range
![Page 11: Chapter 08](https://reader036.fdocuments.us/reader036/viewer/2022062405/5560e877d8b42a3d768b505f/html5/thumbnails/11.jpg)
8-11
For Each/Next Statements
Use Loops to reference each element in the arrayFor / Next or For Each/Next
VB references EACH element of the array and assigns its value to ElementNameVariable used for ElementName must be same datatype
as array elements or an Object datatypeBest to declare the variable for ElementName as part of
the For Each statement to create a block-level variableMakes one pass through the loop per elementUse Exit For statement within loop to exit early
![Page 12: Chapter 08](https://reader036.fdocuments.us/reader036/viewer/2022062405/5560e877d8b42a3d768b505f/html5/thumbnails/12.jpg)
8-12
The For Each and Next Statements General Form
For Each ElementName [As Datatype] In ArrayName
' Statement(s) in loop.
Next [ElementName]
![Page 13: Chapter 08](https://reader036.fdocuments.us/reader036/viewer/2022062405/5560e877d8b42a3d768b505f/html5/thumbnails/13.jpg)
8-13
The For Each and Next Statements Example
For Each eachNameString In nameString' Write one element of the array.Debug.WriteLine(eachNameString)
Next eachNameString
![Page 14: Chapter 08](https://reader036.fdocuments.us/reader036/viewer/2022062405/5560e877d8b42a3d768b505f/html5/thumbnails/14.jpg)
8-14
Structures
Combine multiple fields of data to create a new structure
Similar to defining a new data typeCombine fields into a structure using the
Structure, End StructureStructure Declaration (by default a Structure is Public)
Cannot be declared inside a procedureGenerally place at the top of a file with module-
level declarationsCan also be placed in a separate file
![Page 15: Chapter 08](https://reader036.fdocuments.us/reader036/viewer/2022062405/5560e877d8b42a3d768b505f/html5/thumbnails/15.jpg)
8-15
The Structure and End Structure Statements - General Form
[Public|Private|Friend] Structure NameOfStructure
Dim FirstField As Datatype
Dim SecondField As Datatype
. . .
End Structure
![Page 16: Chapter 08](https://reader036.fdocuments.us/reader036/viewer/2022062405/5560e877d8b42a3d768b505f/html5/thumbnails/16.jpg)
8-16
The Structure and End Structure Statements – Example (1 of 2)
Structure EmployeeDim lastnameString As StringDim firstNameString As StringDim socialSecurityNumberString As StringDim streetString As StringDim stateString As StringDim zipCodeString As StringDim hireDate As DateDim payCodeInteger As Integer
End Structure
![Page 17: Chapter 08](https://reader036.fdocuments.us/reader036/viewer/2022062405/5560e877d8b42a3d768b505f/html5/thumbnails/17.jpg)
8-17
The Structure and End Structure Statements – Example (2 of 2)
Friend Structure ProductDim descriptionString As StringDim productNumberString As StringDim quantityInteger As IntegerDim priceDecimal As Decimal
End Structure
Structure SalesDetail
Dim saleDecimal () As Decimal
EndStructure
![Page 18: Chapter 08](https://reader036.fdocuments.us/reader036/viewer/2022062405/5560e877d8b42a3d768b505f/html5/thumbnails/18.jpg)
8-18
Accessing the Elements in a Structure Variable
Each field of data in Structure is referred to as an element of the structure
To access elements use the dot notation similar to that used for objects--Specify Variable.Element
ExamplesofficeEmployee.lastNameStringofficeEmployee.hireDateinventoryProduct(indexInteger).descriptionStringinventoryProduct(indexInteger).quantityIntegerinventoryProduct(indexInteger).priceDecimal
![Page 19: Chapter 08](https://reader036.fdocuments.us/reader036/viewer/2022062405/5560e877d8b42a3d768b505f/html5/thumbnails/19.jpg)
8-19
Including An Array In A Structure
Arrays can be included as elements within a StructureVB does not allow you to declare the number of
elements in the array within the Structure declarationUse the ReDim statement inside a procedure to define
the size of the array
![Page 20: Chapter 08](https://reader036.fdocuments.us/reader036/viewer/2022062405/5560e877d8b42a3d768b505f/html5/thumbnails/20.jpg)
8-20
ReDim Code Example
' Module-level declarations.Structure SalesDetail
Dim saleDecimal( ) As DecimalEnd Structure
Private houseWaresSalesDetail As SalesDetail
' Inside a procedure.' Establish the number of elements in the array.ReDim houseWaresSalesDetail.saleDecimal(6)
' In processing.houseWaresSalesDetail.saleDecimal _ (dayIndexInteger) = currentDaySalesDecimal
![Page 21: Chapter 08](https://reader036.fdocuments.us/reader036/viewer/2022062405/5560e877d8b42a3d768b505f/html5/thumbnails/21.jpg)
8-21
Using Array Elements for Accumulators
![Page 22: Chapter 08](https://reader036.fdocuments.us/reader036/viewer/2022062405/5560e877d8b42a3d768b505f/html5/thumbnails/22.jpg)
8-22
Debugging Array Programs
View the array elements in debugging time by setting a breakpoint and view the Autos window; click the plus sign to left of array name to view individual array elements
![Page 23: Chapter 08](https://reader036.fdocuments.us/reader036/viewer/2022062405/5560e877d8b42a3d768b505f/html5/thumbnails/23.jpg)
8-23
Table Lookup
Often values used to identify a series of elements are not sequential
Use a table lookup process to find the correct element in the array
Establish a structure and dimension an array of the structure
Use the Form_Load event procedure to put numbers in table-executed once the form is loaded into memory
![Page 24: Chapter 08](https://reader036.fdocuments.us/reader036/viewer/2022062405/5560e877d8b42a3d768b505f/html5/thumbnails/24.jpg)
8-24
Coding a Table Lookup
![Page 25: Chapter 08](https://reader036.fdocuments.us/reader036/viewer/2022062405/5560e877d8b42a3d768b505f/html5/thumbnails/25.jpg)
8-25
Lookup Operation Logic
![Page 26: Chapter 08](https://reader036.fdocuments.us/reader036/viewer/2022062405/5560e877d8b42a3d768b505f/html5/thumbnails/26.jpg)
8-26
Using List Boxes With Arrays (1 of 2)
Use List Boxes or Combo Boxes rather than text boxes to look up information in the array
Use the list's SelectedIndex property to determine the array subscriptSelected Index property holds the position or index of
the selected list item
![Page 27: Chapter 08](https://reader036.fdocuments.us/reader036/viewer/2022062405/5560e877d8b42a3d768b505f/html5/thumbnails/27.jpg)
8-27
Using List Boxes With Arrays (1 of 2)
groupNumberInteger = groupListBox.SelectedIndex
Allow the user to select from a list and the SelectedIndex property can be used as the subscript of the total array.
![Page 28: Chapter 08](https://reader036.fdocuments.us/reader036/viewer/2022062405/5560e877d8b42a3d768b505f/html5/thumbnails/28.jpg)
8-28
Multidimensional Arrays
To define a two-dimensional array or table--Dim statement specifies number of rows and
columnsThe row is horizontal and the column is verticalMay specify number of elements –OR-- initial
valuesSpecify row with first subscript, column with
second subscript, need to use a comma to specify the dimensions
![Page 29: Chapter 08](https://reader036.fdocuments.us/reader036/viewer/2022062405/5560e877d8b42a3d768b505f/html5/thumbnails/29.jpg)
8-29
The Dim Statement for Two-Dimensional Arrays - General Form
Dim ArrayName(HighestSubscript, Highest Subscript) As DatatypeDim ArrayName( , ) As Datatype = {ListOfValues}
![Page 30: Chapter 08](https://reader036.fdocuments.us/reader036/viewer/2022062405/5560e877d8b42a3d768b505f/html5/thumbnails/30.jpg)
8-30
The Dim Statement for Two-Dimensional Arrays – Example(s)
Dim nameString(2, 3) As String
Dim nameString( , ) As String = {{"James", "Mary", "Sammie", "Sean"}, _ {"Tom", "Lee", "Leon", "Larry"}, {"Maria", "Margaret", "Jill", "John"}}
' Both statements establish an array of 12 elements.
(0, 0)James
(0, 1)Mary
(0, 2)Sammie
(0, 3)Sean
(1, 0)Tom
(1, 1)Lee
(1, 2)Leon
(1, 3)Larry
(2, 0)Maria
(2, 1)Margaret
(2, 2)Jill
(2, 3)John
![Page 31: Chapter 08](https://reader036.fdocuments.us/reader036/viewer/2022062405/5560e877d8b42a3d768b505f/html5/thumbnails/31.jpg)
8-31
Initializing Two-Dimensional Arrays
Initializing/Reinitializing Use nested For/Next loop
Printing a Two-Dimensional TableUse For Each/Next loop
Summing a Two-Dimensional TableInclude a total field for each row and each columnSum the figures in both directions (double-check
totals)
![Page 32: Chapter 08](https://reader036.fdocuments.us/reader036/viewer/2022062405/5560e877d8b42a3d768b505f/html5/thumbnails/32.jpg)
8-32
Nested For/Next Example
For rowInteger = 0 To 2For columnInteger = 0 To 3
' Initialize each element.nameString(rowInteger, columnInteger) = " "
Next columnIntegerNext rowInteger
![Page 33: Chapter 08](https://reader036.fdocuments.us/reader036/viewer/2022062405/5560e877d8b42a3d768b505f/html5/thumbnails/33.jpg)
8-33
Printing a Two-Dimensional Table
' Print one name per line.For Each elementString In nameString
' Set up a line. e.Graphics.DrawString(elementString, printFont, _ Brushes.Black, horizontalPrintLocationSingle, _ verticalPrintLocationSingle)' Increment the Y position for the next line.verticalPrintLocationSingle += lineHeightSingle
Next elementString
![Page 34: Chapter 08](https://reader036.fdocuments.us/reader036/viewer/2022062405/5560e877d8b42a3d768b505f/html5/thumbnails/34.jpg)
8-34
Summing a Two-Dimensional Table
![Page 35: Chapter 08](https://reader036.fdocuments.us/reader036/viewer/2022062405/5560e877d8b42a3d768b505f/html5/thumbnails/35.jpg)
8-35
Lookup Operations for Two-Dimensional Tables
Use same techniques as for single dimensional arraysDirect Reference (if meaningful row and column
subscripts are available)Table Lookup
Many 2D tables used for lookup will require additional one-dimensional arrays or lists to aid in the lookup process
![Page 36: Chapter 08](https://reader036.fdocuments.us/reader036/viewer/2022062405/5560e877d8b42a3d768b505f/html5/thumbnails/36.jpg)
8-36
Two Dimensional Array Example