1 / 37

aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf

aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf

darci
Download Presentation

aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf

  2. 8 Chapter Arrays McGraw-Hill © 2006 The McGraw-Hill Companies, Inc. All rights reserved.

  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 data • Accumulate totals using arrays • Distinguish between direct access and indirect access of a table • Write a table lookup for matching an array element • Combine the advantages of list box controls with arrays • Store and look up data in multidimensional arrays

  4. Single-Dimension Arrays • List or series of values all referenced by the same name • Similar to list of values for list boxes and combo boxes - without the box • Use an array to store a series of variables for later processing • Use an array to store multiple values • May be referred to as a table or subscripted (index) variable • Individual elements are treated the same as any other variable and may be used in any statement

  5. Array Terms • Element • Individual item in the array • Subscript (or index) • Zero based number used to reference the specific elements in the array • Must be an integer • Boundaries • Lower Subscript, 0 by default • Upper Subscript

  6. Janet Baker George Lee Sue Li Samuel Hoosier Sandra Weeks William Macy Andy Harrison Ken Ford Denny Franks Shawn James (0) (1) (2) (3) (4) (5) (6) (7) (8) (9) Array Example nameString Array

  7. Subscripts • Subscripts may be constants, variables, or numeric expressions • Subscripts must be integers-VB rounds any noninteger subscript

  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 the upper subscript and initial values PrivateArrayName(UpperSubscript) As Datatype DimArrayName() AsDatatype = {InitialValueList} DimArrayNameAsDatatype( ) = {InitialValueList}

  9. Dim Statement for Arrays Example(s) Dim nameString(25) As String Dim balanceDecimal(10) As Decimal Dim productString(99) As String Dim 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 String Public idNumberString(5) As String

  10. Valid Subscripts • Subscript must reference a valid element of an array • VB rounds fractional subscripts • VB throws exceptions for subscripts that are out of range

  11. The For Each and Next Statements General Form For EachElementName [As Datatype] In ArrayName ' Statement(s) in loop. Next [ElementName] • Use Loops to reference each element in the array • For / Next or For Each/Next

  12. The For Each and Next Statements Example For Each eachNameString In nameString ' Write one element of the array. Debug.WriteLine(eachNameString) Next eachNameString

  13. For Each/Next Statements • VB references EACH element of the array and assigns its value to ElementName • Variable used for ElementName must be same datatype as array elements or an Object datatype • Best to declare the variable for ElementName as part of the For Each statement to create a block-level variable • Makes one pass through the loop per element • Use Exit For statement within loop to exit early

  14. Structures • Combine multiple fields of data to create a new structure • Similar to defining a new data type • Combine fields into a structure using the Structure, End Structure • Structure Declaration (by default a Structure is Public) • Cannot be declared inside a procedure • Generally place at the top of a file with module-level declarations • Can also be placed in a separate file

  15. The Structure and End Structure Statements - General Form [Public|Private|Friend] StructureNameOfStructure Dim FirstField As Datatype Dim SecondField As Datatype . . . End Structure

  16. The Structure and End Structure Statements – Example (1 of 2) StructureEmployee Dim lastnameString As String Dim firstNameString As String Dim socialSecurityNumberString As String Dim streetString As String Dim stateString As String Dim zipCodeString As String Dim hireDate As Date Dim payCodeInteger As Integer End Structure

  17. The Structure and End Structure Statements – Example (2 of 2) FriendStructureProduct Dim descriptionString As String Dim productNumberString As String Dim quantityInteger As Integer Dim priceDecimal As Decimal End Structure Structure SalesDetail Dim saleDecimal () As Decimal EndStructure

  18. Declaring Variables Based on a Structure • Once you have created structure, you can declare variables of the structure, just as if it were another data type • Examples Dim officeEmployee As Employee Dim warehouseEmployee As Employee Dim widgetProduct As Product Dim inventoryProduct(100) As Product Dim houseWaresSalesDetail As SalesDetail Dim homeFurnishingsSalesDetail As SalesDetail

  19. 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 • Examples officeEmployee.lastNameString officeEmployee.hireDate inventoryProduct(indexInteger).descriptionString inventoryProduct(indexInteger).quantityInteger inventoryProduct(indexInteger).priceDecimal

  20. Including An Array In A Structure • Arrays can be included as elements within a Structure • VB does not allow you to declare the number of elements in the array within the Structure declaration • Use the ReDim statement inside a procedure to define the size of the array

  21. 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

  22. Using Array Elements for Accumulators

  23. Using Array Elements for AccumulatorsExample • This technique is called direct reference. With Me ' Convert input group number to subscript groupNumberInteger = Integer.Parse(.groupTextBox.Text) – 1 ‘Add sale to the correct total saleInteger = Integer.Parse(.saleTextBox.Text) totalInteger(groupNumberInteger) += saleInteger End With

  24. 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

  25. 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 • Check code on pages 330 – 332 for example

  26. Coding a Table Lookup

  27. Lookup Operation Logic

  28. 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 subscript • Selected Index property holds the position or index of the selected list item • Check example code on page 333

  29. Using List Boxes With Arrays (1 of 2) Allow the user to select from a list and the SelectedIndex property can be used as the subscript of the total array. groupNumberInteger = groupListBox.SelectedIndex

  30. Multidimensional Arrays To define a two-dimensional array or table-- • Dim statement specifies number of rows and columns • The row is horizontal and the column is vertical • May specify number of elements –OR-- initial values • Specify row with first subscript, column with second subscript, need to use a comma to specify the dimensions

  31. The Dim Statement for Two-Dimensional Arrays - General Form DimArrayName(HighestSubscript, Highest Subscript) AsDatatype DimArrayName( , )AsDatatype = {ListOfValues}

  32. The Dim Statement for Two-Dimensional Arrays – Example(s) DimnameString(2, 3) As String DimnameString( , ) As String = {{"James", "Mary", "Sammie", "Sean"}, _ {"Tom", "Lee", "Leon", "Larry"}, {"Maria", "Margaret", "Jill", "John"}} 'Both statements establish an array of 12 elements.

  33. Initializing Two-Dimensional Arrays • Initializing/Reinitializing • Use nested For/Next loop For rowInteger = 0 To 2 For columnInteger = 0 To 3 ' Initialize each element. nameString(rowInteger, columnInteger) = " " Next columnInteger Next rowInteger

  34. Printing a Two-Dimensional Table • Printing a Two-Dimensional Table • Use For Each/Next loop ' Print one name per line. For EachelementString 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

  35. Summing a Two-Dimensional Table

  36. Lookup Operations for Two-Dimensional Tables • Use same techniques as for single dimensional arrays • Direct Reference (if meaningful row and column subscripts are available) • Example on page 337 • Table Lookup • Example on pages 337-339 • Many 2D tables used for lookup will require additional one-dimensional arrays or lists to aid in the lookup process

  37. Two Dimensional Array Example

More Related