Download presentation
Presentation is loading. Please wait.
Published byAntony Stewart Modified over 8 years ago
1
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. Arrays Chapter 8
2
McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved. 8-2 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 look up for matching an array element. Combine the advantages of list box controls with arrays. Store and look up data in multidimensional arrays.
3
McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved. 8-3 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.
4
McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved. 8-4 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
5
McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved. 8-5 Array Example nameString Array (0) (1) (2) (3) (4) (5) (6) (7) (8) (9) Janet Baker George Lee Sue Li Samuel Hoosier Sandra Weeks William Macy Andy Harrison Ken Ford Denny Franks Shawn James
6
McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved. 8-6 Subscripts Subscripts may be constants, variables, or numeric expressions. Subscripts must be integers — VB rounds any noninteger subscript.
7
McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved. 8-7 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. Private ArrayName(UpperSubscript) As Datatype Dim ArrayName( ) As Datatype = {InitialValueList} Dim ArrayName As Datatype( ) = {InitialValueList}
8
McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved. 8-8 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 IdNumbersString(5) As String
9
McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved. 8-9 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.
10
McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved. 8-10 For Each/Next Statements Use Loops to reference each element in the array. For / Next or For Each/Next 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.
11
McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved. 8-11 The For Each and Next Statements — General Form For Each ElementName [As Datatype] In ArrayName ' Statement(s) in loop. Next [ElementName]
12
McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved. 8-12 The For Each and Next Statements — Example For Each OneNameString As String In NameString ' Write one element of the array. Debug.WriteLine(OneNameString) Next OneNameString
13
McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved. 8-13 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 placed at the top of a file with module-level declarations Can also be placed in a separate file
14
McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved. 8-14 The Structure and End Structure Statements — General Form [Public|Private|Friend] Structure NameOfStructure Dim FirstField As Datatype Dim SecondField As Datatype... End Structure
15
McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved. 8-15 The Structure and End Structure Statements — Example (1 of 2) Structure Employee 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
16
McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved. 8-16 The Structure and End Structure Statements — Example (2 of 2) Friend Structure Product Dim DescriptionString As String Dim ProductNumberString As String Dim QuantityInteger As Integer Dim PriceDecimal As Decimal End Structure Structure SalesDetail Dim SaleDecimal () As Decimal End Structure
17
McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved. 8-17 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
18
McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved. 8-18 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.
19
McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved. 8-19 ReDim Code Example ' Module-level declarations. Structure SalesDetail Dim SaleDecimal( ) As Decimal End 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
20
McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved. 8-20 Using Array Elements for Accumulators
21
McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved. 8-21 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.
22
McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved. 8-22 Table Look up Often, values used to identify a series of elements are not sequential. Use a table look up 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 as the form is loaded into memory.
23
McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved. 8-23 Coding a Table Look up
24
McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved. 8-24 Look-up Operation Logic
25
McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved. 8-25 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. SelectedIndex property holds the position or index of the selected list item.
26
McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved. 8-26 Using List Boxes With Arrays (1 of 2) IndexInteger = GroupListBox.SelectedIndex If IndexInteger <> –1 Then Allow the user to select from a list and the SelectedIndex property can be used as the subscript of the total array.
27
McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved. 8-27 Using List Boxes With Arrays (2 of 2) SaleInteger = Integer.Parse(SaleTextBox.Text) TotalInteger(IndexInteger) += SaleInteger ' Clear the screen fields. GroupListBox.SelectedIndex = –1 SaleTextBox.Text = "" Else MessageBox.Show("Select a group number from the list.", "Data Entry Error", MessageBoxButtons.OK,MessageBoxIcon.Exclamation) End If
28
McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved. 8-28 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 initial values Specify row with first subscript, column with second subscript, and use a comma to specify the dimensions.
29
McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved. 8-29 The Dim Statement for Two-Dimensional Arrays — General Form Dim ArrayName(HighestRowSubscript, Highest ColumnSubscript) As Datatype Dim ArrayName(, ) As Datatype = {ListOfValues}
30
McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved. 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
31
McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved. 8-31 Initializing Two-Dimensional Arrays Initializing/Reinitializing Use nested For/Next loop. Printing a Two-Dimensional Table Use For Each/Next loop. Summing a Two-Dimensional Table Include a total field for each row and each column. Sum the figures in both directions (double-check totals).
32
McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved. 8-32 Nested For/Next Example For RowInteger As Integer= 0 To 2 For ColumnInteger As Integer= 0 To 3 ' Initialize each element. NameString(RowInteger, ColumnInteger) = " " Next ColumnInteger Next RowInteger
33
McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved. 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
34
McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved. 8-34 Summing a Two-Dimensional Table
35
McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved. 8-35 Look-up Operations for Two- Dimensional Tables Use same techniques as for single dimensional arrays – Direct Reference (if meaningful row and column subscripts are available) – Table Look up Many 2D tables used for look up will require additional one-dimensional arrays or lists to aid in the look-up process.
36
McGraw-Hill© 2009 The McGraw-Hill Companies, Inc. All rights reserved. 8-36 Two Dimensional Array Example
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.