Working with data - PowerPoint PPT Presentation

1 / 23
About This Presentation
Title:

Working with data

Description:

A variable (or simple variable) is a name to which Visual Basic can assign a single value. ... array having upper bound N and assigns value0 to arrayName(0), value1 to ... – PowerPoint PPT presentation

Number of Views:40
Avg rating:3.0/5.0
Slides: 24
Provided by: mil94
Category:
Tags: assign | data | working

less

Transcript and Presenter's Notes

Title: Working with data


1
Working with data
  • CS0004
  • Lecture 11
  • Chapter 6.2 (review) and 7.1

2
Administration
  • Project 2 is posted
  • A bit harder but similar to last project
  • Use random number for dice roll
  • Use functions to break tasks into smaller pieces
  • Use loops to play like a computer
  • Due next Wednesday (get working on it)

3
Reading Data From Files
  • StreamReader
  • Dim name As String
  • Dim wage, hours As Double
  • Dim sr As IO.StreamReader _
  • IO.File.OpenText("PAYROLL.TXT")
  • name sr.ReadLine
  • wage CDbl(sr.ReadLine)
  • hours CDbl(sr.ReadLine)

4
Lets Do Something
  • Given a file containing phone numbers lets create
    a program that searches through the file for a
    given name and displays the phone number.

5
Recap
  • Variables (Dim x As Integer)
  • If statements
  • Select Case Statements
  • Procedures and Function
  • Do While Loop
  • Do Until Loop
  • For Next
  • StreamReader

6
Simple and Array Variables
  • A variable (or simple variable) is a name to
    which Visual Basic can assign a single value.
  • An array variable is a collection of simple
    variables of the same type to which Visual Basic
    can assign a list of values.

7
Example
  • Suppose you wanted to evaluate exam scores for a
    class of 30 students.
  • What would you need to store each of these values?

Dim score1 As DoubleDim score2 As DoubleDim
score3 As Double Dim score30 As Double
8
Using Arrays
  • Instead of creating a simple variable for each
    students scores we can use an array to store a
    list of scores
  • Think of an array as just a list of numbers

9
Using Arrays
Size of the array (ex 30 student scores)
Dim student_score(29) As Double
Array name
Data type
  • 29 is actually an upper bound on the array
  • Starts at 0 not 1 so size is actually 29 1 30

10
Using Arrays
student_store(0) 0.93
subscript
Read "student sub zero equals 0.93" Which means
that the double 0.93 is being stored at the first
location in the array called student_score
because all arrays begin counting at 0.
11
Array Terms
  • Dim arrayName(n) As DataType
  • 0 is the "lower bound" of the array
  • n is the "upper bound" of the array the last
    available subscript in this array
  • The number of elements, n 1, is the size of the
    array

12
Example
txtNumber
txtWinner
13
Example
  • Private Sub btnWhoWon_Click(...) _
  • Handles
    btnWhoWon.Click
  • Dim teamName(3) As String
  • Dim n As Integer
  • 'Place Super Bowl Winners into the array
  • teamName(0) "Packers"
  • teamName(1) "Packers"
  • teamName(2) "Jets"
  • teamName(3) "Chiefs"
  • 'Access array
  • n CInt(txtNumber.Text)
  • txtWinner.Text teamName(n - 1)
  • End Sub

14
Example Output
15
Initializing Arrays
  • Arrays may be initialized when they are created
  • Dim arrayName() As varType value0,value1,value2
    , ..., valueN
  • declares an array having upper bound N and
    assigns value0 to arrayName(0), value1 to
    arrayName(1), ..., and valueN to arrayName(N).

16
Getting the upper bound
  • The value of
  • arrayName.GetUpperBound(0)
  • is the upper bound of arrayName().

17
Example
  • Dim teamName() As String "Packers",
    "Packers", "Jets", "Chiefs"
  • txtBox.Text CStr(teamName.GetUpperBound(0))
  • Output 3

18
ReDim Command
  • The size of an array may be changed after
  • it has been created.
  • ReDim arrayName(m)
  • where arrayName is the name of the
  • already declared array and m is an Integer
  • literal, variable, or expression, changes the
  • upper bound of the array to m.

19
Preserve Contents (ReDim)
  • ReDim arrayName(m)
  • resets all values to their default. This can be
    prevented with the keyword Preserve.
  • ReDim Preserve arrayName(m)
  • resizes the array and retains as many
  • values as possible.

20
Counting Letters
Page 315 in book
21
Out of Bounds Error
  • The following code references an array element
  • that doesn't exist. This will cause an error.
  • Dim trees() As String "Sequoia",
  • "Redwood","Spruce"
  • txtBox.Text trees(5)

22
Copying Arrays
  • If arrayOne() and arrayTwo() have been declared
  • with the same data type, then the statement
  • arrayOne arrayTwo
  • makes arrayOne() an exact duplicate of
  • arrayTwo(). It will have the same size and
  • contain the same information.

23
Erasing Arrays
  • Arrays take up large amount of room
  • Sometimes you want to delete them
  • Erase arrayName
Write a Comment
User Comments (0)
About PowerShow.com