Arrays: Programming in
Arrays: Programming in
Arrays
Programming In
Visual Basic.NET
Case Structure
[block of code]
End Sub
Example
' Assumes array intTotal previously dimensioned
Dim intOneTotal As Integer ‘must be the same type as
intTotal
For Each intOneTotal In intTotal ‘references EACH
element
intOneTotal=0 ' reinitialize element of the
array
8- 15
Next intOneTotal © 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
Example
Structure CoffeeSale
Dim strType As String
Dim strQuantity As String
Dim decPrice As Decimal
End Structure
Dim mcslTransaction(20) As CoffeeSale
8- 17 © 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).decPric
e
• Examples
ReDim Preserve strDept(20)
ReDim sdtHousewares.decSale(6)
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)
decColTotal(5)
0 1 2 3 4 5
8- 28 © 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- 29 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved.
Lookup Two-Dimensional
Tables
Lookup for an intersection cell
(VBMailOrder example)