chapter 08

36
8-1 lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh lskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjh

Upload: terry-yoast

Post on 24-May-2015

587 views

Category:

Education


8 download

TRANSCRIPT

Page 1: Chapter 08

8-1

aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf

Page 2: Chapter 08

Arrays

Chapter 88

McGraw-Hill © 2006 The McGraw-Hill Companies, Inc. All rights reserved.

Page 3: Chapter 08

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

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

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

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

8-7

Subscripts

Subscripts may be constants, variables, or numeric expressionsSubscripts must be integers-VB rounds any noninteger

subscript

Page 8: Chapter 08

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

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

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

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

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

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

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

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

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

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

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

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

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

8-21

Using Array Elements for Accumulators

Page 22: Chapter 08

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

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

8-24

Coding a Table Lookup

Page 25: Chapter 08

8-25

Lookup Operation Logic

Page 26: Chapter 08

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

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

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

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

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

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

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

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

8-34

Summing a Two-Dimensional Table

Page 35: Chapter 08

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

8-36

Two Dimensional Array Example