slide1 n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf PowerPoint Presentation
Download Presentation
aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf

Loading in 2 Seconds...

play fullscreen
1 / 37

aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf - PowerPoint PPT Presentation


  • 101 Views
  • Uploaded on

aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf' - darci


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
slide1

aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhfaslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf

aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf

aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf

aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf

aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf

aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf

aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf

aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf

aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf

aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf

aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf

aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf

aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf

aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf

aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf

aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf

aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf

aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf

aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf

aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf

aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf

aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf

aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf

aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf

arrays

8

Chapter

Arrays

McGraw-Hill

© 2006 The McGraw-Hill Companies, Inc. All rights reserved.

objectives
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
single dimension arrays
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
array terms
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
array example

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

subscripts
Subscripts
  • Subscripts may be constants, variables, or numeric expressions
    • Subscripts must be integers-VB rounds any noninteger subscript
the declaration statements for arrays general form
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}

dim statement for arrays example s
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

valid subscripts
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
the for each and next statements general form
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
the for each and next statements example
The For Each and Next Statements Example

For Each eachNameString In nameString

' Write one element of the array.

Debug.WriteLine(eachNameString)

Next eachNameString

for each next statements
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
structures
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
the structure and end structure statements general form
The Structure and End Structure Statements - General Form

[Public|Private|Friend] StructureNameOfStructure

Dim FirstField As Datatype

Dim SecondField As Datatype

. . .

End Structure

the structure and end structure statements example 1 of 2
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

the structure and end structure statements example 2 of 2
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

declaring variables based on a structure
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

accessing the elements in a structure variable
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

including an array in a structure
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
redim code example
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

using array elements for accumulators example
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

debugging array programs
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
table lookup
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
using list boxes with arrays 1 of 2
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
using list boxes with arrays 1 of 21
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

multidimensional arrays
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
the dim statement for two dimensional arrays general form
The Dim Statement for Two-Dimensional Arrays - General Form

DimArrayName(HighestSubscript, Highest Subscript) AsDatatype

DimArrayName( , )AsDatatype = {ListOfValues}

the dim statement for two dimensional arrays example s
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.

initializing two dimensional arrays
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

printing a two dimensional table
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

lookup operations for two dimensional tables
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