Download presentation
Presentation is loading. Please wait.
1
Programming In Visual Basic .NET
Chapter 8 Arrays Programming In Visual Basic .NET
2
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 multiple values May be referred to as a table or subscripted variable © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
3
Array Terms Element Subscript (or index) Boundaries
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 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
4
Array Example nameString Array (0) Janet Baker (1) George Lee (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 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
5
The Dim Statement for Arrays - General Form
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 Datatype Dim ArrayName( ) As Datatype = {InitialValueList} Dim ArrayName As Datatype( ) = {InitialValueList} © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
6
Dim Statement for Arrays Examples
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 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
7
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 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
8
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 Makes one pass through the loop per element Use Exit For statement within loop to exit early © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
9
The For Each and Next Statements – General Form
For Each ElementName In ArrayName ' Statement(s) in loop. Next [ElementName] © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
10
The For Each and Next Statements - Example
' Assumes array nameString is dimensioned and holds data. Dim eachNameString As String For Each eachNameString In nameString ' Write one element of the array. Debug.WriteLine(eachNameString) Next eachNameString © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
11
Structures Combine multiple fields of data to create a new structure
Similar to defining a new data type Define using Structure, End Structure Declaration (by default a Structure is Public) Cannot be declared inside a procedure Generally place with module-level declarations © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
12
The Structure and End Structure Statements - General Form
[Public|Private] Structure NameOfStructure Dim FirstField As Datatype Dim SecondField As Datatype . . . End Structure © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
13
The Structure and End Structure Statements - Example
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 ' Declaring a variable based on the Structure. Dim officeEmployee As Employee © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
14
The Structure and End Structure Statements - Example
Public Structure Product Dim descriptionString As String Dim idString As String Dim quantityInteger As Integer Dim priceDecimal As Decimal End Structure ' Declaring a variable array based on the structure. Dim inventoryProduct(100) As Product © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
15
Accessing the Elements in a Structure Variable
Each field of data in Structure is an Element To access specify Variable.Element Examples officeEmployee.lastNameString officeEmployee.hireDate inventoryProduct(indexInteger).descriptionString inventoryProduct(indexInteger).quantityInteger inventoryProduct(indexInteger).priceDecimal © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
16
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 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
17
ReDim Code Example ' Module-level declarations. Structure SalesDetail Dim saleDecimal( ) As Decimal End Structure Dim houseWaresSalesDetail As SalesDetail ' Inside a procedure. ' Establish the number of elements in the array. ReDim houseWaresSalesDetail.saleDecimal(6) ' In processing. houseWaresSalesDetail.saleDecimal _ (dayIndexInteger) = currentDaySalesDecimal © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
18
Using Array Elements for Accumulators (pgs. 325-327)
groupNumberInteger=(groupTextBox.Text - 1) totalInteger array (0) (1) (2) (3) (4) (5) (6) (7) 10 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
19
Debugging Array Programs
View the array elements in break time in the Autos window, click plus sign to left of array name to view individual array elements © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
20
Table Lookup (pgs ) 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 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
21
Table Lookup (continued)
indexInteger=2 arrayGroup array (0) (1) (2) (3) (4) (5) (6) (7) 101 0 103 0 110 10 115 0 121 0 123 0 130 0 145 0 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
22
Using List Boxes With Arrays
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 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
23
Using List Boxes With Arrays (continued)
groupNumberInteger = groupListBox.SelectedIndex © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
24
Multidimensional Arrays
Dim statement specifies number of rows and columns in two-dimensional array May specify number of elements OR initial values Specify row with first subscript, column with second subscript © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
25
The Dim Statement for Two-Dimensional Arrays - General Form
Dim ArrayName(HighestSubscript, Highest Subscript) As Datatype Dim ArrayName( , ) As Datatype = {ListOfValues} © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
26
The Dim Statement for Two-Dimensional Arrays - Examples
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 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
27
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) © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
28
Nested For/Next Example
Dim rowInteger As Integer Dim columnInteger As Integer For rowInteger = 0 To 2 For columnInteger = 0 To 3 ' Initialize each element. nameString(rowInteger, columnInteger) = " " Next columnInteger Next rowInteger © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
29
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 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
30
Summing a Two-Dimensional Table
amountDecimal (0) (1) (2) (3) (4) (5) rowTotalDecimal (0) (1) (2) (3) (0) (1) (2) (3) ROW TOTALS COLUMN TOTALS columnTotalDecimal (0) (1) (2) (3) (4) (5) © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
31
Summing Code Example ' Crossfoot Total a 2D table.
Dim amountDecimal( , ) As Decimal = {{2.5D, 3D, 1.2D, 2.2D, 4.5D, 3.5D}, _ {2D, 2D, 2D, 2D, 2D, 2D}, {3D, 3.1D, 3.2D, 3.3D, 3.4D, 3.5D}, _ {4.4D, 4.5D, 4.6D, 4.7D, 4.8D, 4.9D}} Dim rowTotalDecimal(3) As Decimal Dim columnTotalDecimal(5) As Decimal Dim rowInteger As Integer Dim columnInteger As Integer For rowInteger = 0 to 3 For columnInteger = 0 to 5 rowTotalDecimal(rowInteger) += amountDecimal(rowInteger, columnInteger) columnTotalDecimal(columnInteger) += amountDecimal(rowInteger, columnInteger) Next columnInteger Next rowInteger © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
32
Lookup Operations for Two-Dimensional Tables
Use same techniques as for single dimensional arrays Direct 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 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
33
Lookup Example Using List Box (page 335)
weightIndexInteger uses weightListBox.SelectedIndex zoneIndexInteger uses zoneListBox.SelectedIndex © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.