Chapter 08

Post on 24-May-2015

584 views 8 download

Tags:

Transcript of Chapter 08

8-1

aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf

Arrays

Chapter 88

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

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

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

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

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

8-7

Subscripts

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

subscript

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}

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

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

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

8-12

The For Each and Next Statements General Form

For Each ElementName [As Datatype] In ArrayName

' Statement(s) in loop.

Next [ElementName]

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

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

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

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

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

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

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

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

8-21

Using Array Elements for Accumulators

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

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

8-24

Coding a Table Lookup

8-25

Lookup Operation Logic

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

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.

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

8-29

The Dim Statement for Two-Dimensional Arrays - General Form

Dim ArrayName(HighestSubscript, Highest Subscript) As DatatypeDim ArrayName( , ) As Datatype = {ListOfValues}

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

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)

8-32

Nested For/Next Example

For rowInteger = 0 To 2For columnInteger = 0 To 3

' Initialize each element.nameString(rowInteger, columnInteger) = " "

Next columnIntegerNext rowInteger

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

8-34

Summing a Two-Dimensional Table

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

8-36

Two Dimensional Array Example