0% found this document useful (0 votes)
8 views

Arrays

Uploaded by

bahatijeremiah10
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Arrays

Uploaded by

bahatijeremiah10
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 40

Arrays

Programming In
Visual Basic.NET
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 keep a series of variable for
later processing such as
– Reordering
– Calculating
– Printing

8- 2 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved.


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

8- 3 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved.


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

8- 4 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved.


Defining Arrays

• Use Dim statement to declare


• Specify the number of elements in the array
as the UpperSubscript
• Each element of the array will be assigned a
default value
– Numeric ==> 0
– String ==> empty string, 0 characters

8- 5 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved.


Defining Arrays - Alternate
Form
• Optionally, the elements in the array may
be assigned values in the Dim statement
• However, if values are assigned, you cannot
declare the Upper Subscript

8- 6 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved.


General Form Dim Statement
for Arrays

Dim ArrayName(UpperSubscript) as
Datatype

Dim ArrayName( ) as
Datatype = {InitialValueList}

Line continuation not shown on this slide

8- 7 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved.


Dim Statement for Arrays
Examples - Default Values
Dim strName(3) as String
Results in an array of 4 elements:
strName(0), strName(1),
strName(2), strName(3)

Dim decBalance(99) as Decimal


Results in an array of 100 elements:
decBalance(0), . . . ,
8- 8
decBalance(99)
© 2001 by The McGraw-Hill Companies, Inc. All rights reserved.
Dim Statement for Arrays
Examples - Assigned Values

Dim strDept( ) as String = {"ACT", "MKT", "HR"}


Dim intActCode( ) as Integer = {10, 20, 30, 40}

8- 9 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved.


What does VB do with the
array?
• When the DIM statement for the array is
processed VB sets aside room for it in
memory.
• Ex: Dim strName(3) as String
– VB sets aside a memory location for 4 strings
strName
(0)
(1)
(2)
(3)
8- 10 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved.
Referencing Array Elements

• Use the Index(s) of the Element


strName(row)
(0) Sam Smith strName(0) : "Sam Smith"
(1) Jill Creech strName(1) : "Jill Creech"
(2) Paul Fry strName(2) : "Paul Fry"
(3) Rich Wells strName(3) : "Rich Wells"

8- 11 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved.


Working with Arrays

• Use Loops to reference each element in the


array
– For / Next
– For Each / Next

8- 12 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved.


For Each / Next

• VB references EACH element of the array


• VB assigns its value to ElementName
– The variable used for ElementName must be same
datatype as the array elements or an Object
datatype
• Makes one pass through the loop per element
• Use Exit For statement within loop to exit
early
8- 13 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved.
For Each Loop General Form

For Each ElementName In ArrayName


Statements to execute
Next [ElementName]

8- 14 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved.


For Each / Next Examples
' Assumes array strName previously dimensioned
Dim strOneName As String
For Each strOneName In strName
Debug.WriteLine(strOneName) ' Write one array element
Next strOneName

' Assumes array intTotal previously dimensioned


Dim intOneTotal As Integer
For Each intOneTotal In intTotal
intOneTotal=0 ' reinitialize the array
Next intOneTotal

8- 15 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved.


Structures
• Combine multiple fields of data into a single unit
• Declaration (by default a Structure is Public)
– Cannot be declared inside a procedure
– Generally declared in General Declarations
• Define using Structure, End Structure
• Once created, declare variable of the Structure as
if it were another datatype; make up a meaningful
prefix

8- 16 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved.


Structure/End Structure
General Form
[Public|Private] Structure
NameOfStructure
Dim FirstField As Datatype
Dim SecondField As Datatype
...
End Structure

8- 17 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved.


Structure Example 1
Public Structure Employee
Dim intEmpID As Integer
Dim strLName As String
Dim strFName As String
Dim datHireDate As Date
End Structure

' Declaring a variable based on the Structure


Dim empOffice As Employee
8- 18 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved.
Structure Example 2

Public Structure Product


Dim strDesc As String
Dim strProdID As String
Dim intQuan As Integer
Dim decPrice As Decimal
End Structure

' Declaring a variable array based on the Structure


Dim prdInven(100) As Product
8- 19 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved.
Accessing the Elements in a
Structure Variable
• Each field of data in Structure is an Element
• To access specify Variable.Element
• Examples
empOffice.intEmpID prdInven(intIndex).strDesc
empOffice.strFName prdInven(intIndex).strID
empOffice.strLName prdInven(intIndex).intQuan
empOffice.datHireDate prdInven(intIndex).decPrice

8- 20 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved.


Including An Array In A
Structure
• Arrays can be included as elements within a
Structure
• VB does not, however, 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

8- 21 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved.


ReDim Statement

• Used to redeclare the UpperSubscript/Size of


an array
• Can preserve original data in the array
• General Form (basic)
ReDim [Preserve] ArrayName(UpperSubscript)
• Examples
ReDim Preserve strDept(20)
ReDim sdtHousewares.decSale(6)
8- 22 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved.
Using Array Elements for
Accumulators (p 344 -346)
intGroupNum=CInt(txtGroup.Text)-1
mintTotal array
(0) 0
(1) 0
(2) 0
(3) 10
(4) 0
(5) 0
(6) 0
(7) 0

8- 23 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved.


Debugging Array Programs

• View the array elements in Break Time in


the Autos Window or the Locals Window

8- 24 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved.


Table Lookup (p346 - 350)
• Problem: Often values used to identify a series
of elements are not
– Numerically sequential
– Numerically separated by a constant value
– Numeric at all, may be strings
• Solution: Create another array to hold the
identifying values and use a table lookup
process to find the correct element in the array

8- 25 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved.


Table Lookup (cont.)
intIndex=2

mgiGroup array
(0) 101 0
(1) 103 0
(2) 110 10
(3) 115 0
(4) 121 0
(5) 123 0
(6) 130 0
(7) 145 0

8- 26 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved.


Using List Boxes With Arrays

• Use List Boxes or Combo Boxes rather than


using text boxes for the user to enter data
used for looking up information in the array
• Use the list's SelectedIndex property as the
subscript of the corresponding array

8- 27 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved.


List Boxes With Arrays (cont.)
SelectedIndex = 2
of List Box
mgiGroup array
(0) 101 0
(1) 103 0
(2) 110 10
(3) 115 0
(4) 121 0
(5) 123 0
(6) 130 0
(7) 145 0

8- 28 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved.


Multidimensional Arrays
• Arrays can have more than one dimension
• Like a table of values
• You must specify the boundaries for each
dimension using subscripts
• Example: Two Dimensional Array
Dim intScoreBoard (1, 8) as Integer
0 1 2 3 4 5 6 7 8
0
1

8- 29 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved.


Referencing Elements in
Multidimensional Array
intScoreBoard(row,column)
0 1 2 3 4 5 6 7 8
0 0,0 0,1 0,2 0,3 0,4 0,5 0,6 0,7 0,8
1 1,0 1,1 1,2 1,3 1,4 1,5 1,6 1,7 1,8

intScoreBoard(0,0) intScoreBoard(1,0)
intScoreBoard(0,1) intScoreBoard(1,1)
intScoreBoard(0,2) intScoreBoard(1,2)
intScoreBoard(0,3) intScoreBoard(1,3)
intScoreBoard(0,4) intScoreBoard(1,4)
intScoreBoard(0,5) intScoreBoard(1,5)

8- 30 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved.


General Form Dim Statement
for Two-Dimensional Arrays

Dim ArrayName(HighestSubscript,
Highest Subscript) as Datatype

Dim ArrayName( , ) as
Datatype = {ListOfValues}

Line continuation not shown on this slide

8- 31 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved.


Dim Statement for Two
Dimensional-Arrays Examples
Dim strName(2, 3) as String
Results in an array of 12 elements:
3 rows: 0, 1, 2, 3
4 columns: 0, 1, 2, 3, 4
Dim strName( , ) as String = {{"Jim", "Mary", "Sam", "Sean"},
{"Tom", "Sue", "Fred", "Paul"}, {"Tim", "Al", "Bob", "Pete"},
{"Joy", "Wes", "Kim", "Beth"}}
Results in same array as above with default values
Line continuation not shown on this slide

8- 32 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved.


Working With
Two-Dimensional Arrays
• Initializing/Reinitializing
– Use Nested For/Next Loops
– Use For Each/Next Loop
• Printing
– Use For Each/Next Loop
• Summing
– Include a total field for each row and each column
– Use For/Next Loop to calculate sums
8- 33 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved.
Initializing For/Next Example

Dim intRow As Integer


Dim intColumn As Integer
For intRow = 0 to 2
For intColumn = 0 to 3
strName(intRow, intColumn) = " "
Next intColumn
Next intRow

8- 34 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved.


Initializing For Each/Next
Example

Dim strElement As String


For Each strElement In strName
strElement = " "
Next strElement

8- 35 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved.


Printing Example
'Print one name per line
For Each strElement In strName
'Set up a line
e.Graphics.DrawString(strElement, fntPrintFont, _
Brushes.Black, sngPrintX, sngPrintY)
'Increment the Y position for the next line
sngPrintY += sngLineHeight
Next strElement

8- 36 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved.


Summing Example
decAmount(3,5)
0 1 2 3 4 5 decRowTotal(3)
0 0
ROW 1
1
TOTALS
2 2
3 3
COLUMN
TOTALS

decColTotal(5)
0 1 2 3 4 5
8- 37 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved.
Summing Code Example
'Crossfoot Total a 2D table
Dim decAmount(3,5) As Decimal
Dim decRowTotal(3) As Decimal
Dim decColTotal(5) As Decimal
Dim intRowIndex As Integer
Dim intColIndex As Integer

For intRowIndex = 0 to 3
For intColIndex = 0 to 5
decRowTotal(intRowIndex) += decAmount(intRowIndex, intColIndex)
decColTotal(intColIndex) += decAmount(intRowIndex, intColIndex)
Next intColIndex
Next intRowIndex

8- 38 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved.


Lookup Two-Dimensional
Tables
• Use same techniques as for single
dimension 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 list boxes to aid in the lookup process
8- 39 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved.
Lookup Example Using List
Box (p 356)
Weight Subscript
of Array uses
lstWeight.SelectedIndex

Zone Subscript
of Array uses
lstZone.SelectedIndex

8- 40 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved.

You might also like