chapter 8 - visual basic schneider 1 chapter 3-b files and functions

53
8 - Chapter Visual Basic Schneider 1 Chapter 3-B Files and Functions

Upload: lillian-walker

Post on 14-Jan-2016

234 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Chapter 8 - Visual Basic Schneider 1 Chapter 3-B Files and Functions

Chapter 8 - Visual Basic Schneider 1

Chapter 3-B

Files and Functions

Page 2: Chapter 8 - Visual Basic Schneider 1 Chapter 3-B Files and Functions

Chapter 3 - Visual Basic Schneider

Reading Data from Files

1. Choose a number to be the reference number for the file

2. Execute an Open statement3. Read the data sequentially using Input #

statements4. Close the file

Page 3: Chapter 8 - Visual Basic Schneider 1 Chapter 3-B Files and Functions

Chapter 3 - Visual Basic Schneider

Example of Reading from a File:

Open “DATA.TXT” For Input As #1 Input #1, num1 Input #1, num2 picOutput.Print num1+num2 7Close #1

Reference number

Read the data and assign it to num1

Open the file

Close the file 3

4

Data.txt

Page 4: Chapter 8 - Visual Basic Schneider 1 Chapter 3-B Files and Functions

Chapter 3 - Visual Basic Schneider

Example of Reading from a File:

Open “DATA.TXT” For Input As #1 Input #1, num1, num2 picOutput.Print num1+num2 7Close #1

2 4 5856

math.txt

3

4

Data.txt

Page 5: Chapter 8 - Visual Basic Schneider 1 Chapter 3-B Files and Functions

Chapter 3 - Visual Basic Schneider

Reading Data from Files

Files can be also used for output rather than input. More about files will be discussed in chapter 8 and 9.

Page 6: Chapter 8 - Visual Basic Schneider 1 Chapter 3-B Files and Functions

Chapter 3 - Visual Basic Schneider

Input Dialog Box

An input dialog box can be used to obtain a single item of input from the user

Presents a window (dialog box) requesting input

Syntax: stringVar = InputBox(prompt, title)

Note:The type of inputBox is string type

Page 7: Chapter 8 - Visual Basic Schneider 1 Chapter 3-B Files and Functions

Chapter 3 - Visual Basic Schneider

Using Message Dialog Box for Output

The message dialog box is used to present a pop-up window containing information for the user

Syntax: MsgBox prompt, , title

Page 8: Chapter 8 - Visual Basic Schneider 1 Chapter 3-B Files and Functions

Example of a Message Dialog Box

MsgBox "CS116", , "Visual Basic"

Stays on thescreen until the user presses OK

Page 9: Chapter 8 - Visual Basic Schneider 1 Chapter 3-B Files and Functions

Example of a Message Dialog Box

MsgBox "CS116"

Page 10: Chapter 8 - Visual Basic Schneider 1 Chapter 3-B Files and Functions

Example

Page 11: Chapter 8 - Visual Basic Schneider 1 Chapter 3-B Files and Functions

Chapter 3 - Visual Basic Schneider

Formatting the Output:

Create easily readable output In the Print method, the spacing of the

output is controlled by the following devices:•semicolon

•comma

•Tab function

Page 12: Chapter 8 - Visual Basic Schneider 1 Chapter 3-B Files and Functions

Chapter 3 - Visual Basic Schneider

Semicolons

The next value output is placed in the next column position.

Example: picOutput.Print “Patrick”; ”Jon”

Output: PatrickJon

Page 13: Chapter 8 - Visual Basic Schneider 1 Chapter 3-B Files and Functions

Chapter 3 - Visual Basic Schneider

Example of Semicolon

picOutput.Print “Patrick”; “ Jon”

Output Screen:Patrick Jon

Space here

Space here

Page 14: Chapter 8 - Visual Basic Schneider 1 Chapter 3-B Files and Functions

Chapter 3 - Visual Basic Schneider

Example of Semicolon

picOutput.Print 100; -200; 300

Output Screen:100 -200 300

One space

Two spaces

Page 15: Chapter 8 - Visual Basic Schneider 1 Chapter 3-B Files and Functions

Chapter 3 - Visual Basic Schneider

Commas

A comma in a Print method causes the next value output to be placed in the next available print zone.

Each print zone is 14 positions wide.

Page 16: Chapter 8 - Visual Basic Schneider 1 Chapter 3-B Files and Functions

Chapter 3 - Visual Basic Schneider

Using Commas

Example:picOutput.Print “SEE”, ”YOU”, ”SOON”

Output Screen:SEE YOU SOON

Column 1

Column 15

Column 29

Page 17: Chapter 8 - Visual Basic Schneider 1 Chapter 3-B Files and Functions

Chapter 3 - Visual Basic Schneider

Using Commas

Example:picOutput.Print “abc123def456ghi”,

”whatever”

Output Screen:abc123def456ghi whatever

Column 1

Column 15

Column 29

Page 18: Chapter 8 - Visual Basic Schneider 1 Chapter 3-B Files and Functions

Chapter 3 - Visual Basic Schneider

Using Commas

A print zone can be skipped by typing consecutive commas

Example: picOutput.Print “HOURLY”, , “PAY”

Output Screen: HOURLY PAY

Column 29

Page 19: Chapter 8 - Visual Basic Schneider 1 Chapter 3-B Files and Functions

Cls: clears the form of all textPicBox.Cls: clears the PicBox of all text

Page 20: Chapter 8 - Visual Basic Schneider 1 Chapter 3-B Files and Functions

Private Sub cmdAdd_Click()picoutput.Print

"1234567890123456789"picoutput.Print 10; 20;picoutput.Print -10, 30,picoutput.Printpicoutput.Print 15; ; 6picoutput.Print "Hello"; "hi";picoutput.Printpicoutput.Print "12345", "12" End Sub

; in the next position column position, in the next available print zonepicOutput.Print moves the cursor to the beginning of the next line

Page 21: Chapter 8 - Visual Basic Schneider 1 Chapter 3-B Files and Functions

Chapter 3 - Visual Basic Schneider

Tab Function

Specifies the column where output will start

Use only semicolons with the Tab function

Can only be used to advance the print position (cannot move backwards)

Page 22: Chapter 8 - Visual Basic Schneider 1 Chapter 3-B Files and Functions

Chapter 3 - Visual Basic Schneider

Example of Tab Function

Example: picOutput.Print Tab(3); “Hi there!” ; Tab(25) ;“Bye!”

Output Screen: Hi there! Bye!

Column 3

Column 25

Page 23: Chapter 8 - Visual Basic Schneider 1 Chapter 3-B Files and Functions

Chapter 3 - Visual Basic Schneider

Example of Tab Function

Example: picOutput.Print Tab(3); “Hi there!” ;

Tab(5) ;“Bye!”Because column 5 is already occupied by the

previous string, the output will be at the next line

Output Screen: Hi there! Bye! Column 3 Column 5

Page 24: Chapter 8 - Visual Basic Schneider 1 Chapter 3-B Files and Functions

Tab

Page 25: Chapter 8 - Visual Basic Schneider 1 Chapter 3-B Files and Functions

Chapter 3 - Visual Basic Schneider

Built-In Functions

Take one or more input values and return an output value

A means provided by Visual Basic for carrying out small, common tasks

Types of Built-In functions•Numeric functions (manipulate numbers)

•String functions (manipulate strings)

Page 26: Chapter 8 - Visual Basic Schneider 1 Chapter 3-B Files and Functions

Chapter 3 - Visual Basic Schneider

Numeric Functions

Rnd Returns a number between 0 and 1 (excluding 1)

Sqr(n) Returns the square root of the number n

Round(n,r) Returns the number n rounded to r decimal places

Int(n) Returns the greatest integer less than or equal to the number n

Page 27: Chapter 8 - Visual Basic Schneider 1 Chapter 3-B Files and Functions

Chapter 3 - Visual Basic Schneider

Example of Numeric Functions

Private Sub cmdEvaluate_Click() Dim n As Single, root As Single

n = 6.76 root = Sqr(n) picResults.Print root; Int(n);

Round(n,1)End Sub

Output: 2.6 6 6.8

Page 28: Chapter 8 - Visual Basic Schneider 1 Chapter 3-B Files and Functions
Page 29: Chapter 8 - Visual Basic Schneider 1 Chapter 3-B Files and Functions

Something about Round with number 5

Round( 123.75 , 1) Round(12.45 , 1) Round(3.5 , 0) Round(4.5 , 0) Round(4.52 , 0) Round(23.57, 1 )

123.8

Note that to round a number that is followed by number 5 and nothing after number 5, if it is odd make the round, if it is even number don’t round

12.4

4

4

5

23.6

Page 30: Chapter 8 - Visual Basic Schneider 1 Chapter 3-B Files and Functions

Chapter 3 - Visual Basic Schneider

Commonly-Used String Functions

Function: Left(“Penguin”, 4) Purpose: Returns the number of

characters specified, starting at the beginning of the string

Peng

Page 31: Chapter 8 - Visual Basic Schneider 1 Chapter 3-B Files and Functions

Chapter 3 - Visual Basic Schneider

Commonly-Used String Functions

Function: Right(“Gotham City”, 4)

Purpose: Returns the number of characters specified from the end of the string

City

Page 32: Chapter 8 - Visual Basic Schneider 1 Chapter 3-B Files and Functions

Chapter 3 - Visual Basic Schneider

Commonly-Used String Functions

Function: Mid(“Commissioner”, 4, 3)

Purpose: Returns the substring starting at the position indicated by the first number and continuing for the length specified by the second number

mis

Page 33: Chapter 8 - Visual Basic Schneider 1 Chapter 3-B Files and Functions

Chapter 3 - Visual Basic Schneider

Commonly-Used String Functions

Function: UCase(“Yes”)

Purpose: Converts any lowercase letters in a string to uppercase

YES

Page 34: Chapter 8 - Visual Basic Schneider 1 Chapter 3-B Files and Functions

Chapter 3 - Visual Basic Schneider

String-Related Numeric Functions

Function: InStr(“John Smith”, “m”)

Purpose: Searches for the first occurrence of one string in another and gives the position at which the string is found

7

Page 35: Chapter 8 - Visual Basic Schneider 1 Chapter 3-B Files and Functions

Chapter 3 - Visual Basic Schneider

String-Related Numeric Function

Function: Len(“John Smith”)

Purpose: Returns the number of characters in the string.

10

Page 36: Chapter 8 - Visual Basic Schneider 1 Chapter 3-B Files and Functions

Strings and string Functions examples

Page 37: Chapter 8 - Visual Basic Schneider 1 Chapter 3-B Files and Functions

Example

Page 38: Chapter 8 - Visual Basic Schneider 1 Chapter 3-B Files and Functions

Example

Page 39: Chapter 8 - Visual Basic Schneider 1 Chapter 3-B Files and Functions

Chapter 3 - Visual Basic Schneider

Format Functions

The format functions provide detailed control of how numbers, dates, and strings are displayed.

Examples•FormatNumber (12345.678, 1) 12,345.7

•FormatCurrency (12345.678, 2) $12,345.68

•FormatPercent (.185, 2) 18.50%

•FormatNumber (1 + Sqr(2), 3) 2.414

Page 40: Chapter 8 - Visual Basic Schneider 1 Chapter 3-B Files and Functions

Chapter 3 - Visual Basic Schneider

Rnd Function

Returns a random number from 0 to 1.(excluding 1).

Example:picBox.Print Rnd

Output: Displays a random number from 0 to 1 (0 included and 1 excluded).

Example:picBox.Print Rnd +5

Output: Displays a random number from 5 to 6 (5 included and 6 excluded).

Page 41: Chapter 8 - Visual Basic Schneider 1 Chapter 3-B Files and Functions

Chapter 3 - Visual Basic Schneider

Rnd Function

Example:picBox.Print Int(Rnd)

Output: Displays 0.

Example:picBox.Print Int(Rnd +5)

Output: Displays 5.

Example:picBox.Print Int(Rnd) +5

Output: Displays 5.

Page 42: Chapter 8 - Visual Basic Schneider 1 Chapter 3-B Files and Functions

Chapter 3 - Visual Basic Schneider

Rnd Function

Example:picBox.Print Int(5*Rnd)

Output: Displays a random Integer from 0 to 4 (0 and 4 included).

OR Output: Displays a random Integer from 0 to 5 (0 included and

5 excluded)

Example:picBox.Print Int(5*Rnd) +2

Output: Displays a random Integer from 2 to 6 (2 and 6 included).

Page 43: Chapter 8 - Visual Basic Schneider 1 Chapter 3-B Files and Functions

Exercise: Revision

What will be dsiplayed in the picOutput when the user double click (click 2 times) on the command button cmdExample

Page 44: Chapter 8 - Visual Basic Schneider 1 Chapter 3-B Files and Functions

Exercises:

1. Position the location of the form at run time Form layout windows

2. To open a file called “data.txt” located in C:\VB folder you must write

Open “C:\VB\data.txt” for input as #1

3. Which of the following statement assign the content of the input box to a numeric variable XX = Val(InputBox(“Enter the number”))

Page 45: Chapter 8 - Visual Basic Schneider 1 Chapter 3-B Files and Functions

What is the output of the following code?

Dim s as integer, n as integerOpen “data.txt” for Input as #1Input #1,ns=s+nInput #1,ns=s+nInput #1,ns=s+nclose #1print “s=”;s

output: s= 60

102030

data.txt

Page 46: Chapter 8 - Visual Basic Schneider 1 Chapter 3-B Files and Functions

what is output of the following code?

Dim n as integerOpen “data.txt” for Input as #1Input #1,nInput #1,nClose #1Print “n = ”; n

Output:n = 20

102030

data.txt

Page 47: Chapter 8 - Visual Basic Schneider 1 Chapter 3-B Files and Functions

what is output of the following code?

Dim n as integerOpen “data.txt” for Input as #1Input #1,nClose #1Open “data.txt” for Input as #1Input #1,nClose #1Print “n = ”; n

Output:n = 10

102030

data.txt

Page 48: Chapter 8 - Visual Basic Schneider 1 Chapter 3-B Files and Functions

Syntax Errors (Compile Errors) Grammatical errors, such as misspelling, are

called syntax error. Syntax error is encountered during compilation (by Complier)

picBox.Primt 3 9W = 5 picBox.Print 2- If x > 10

x = x+1End If

Page 49: Chapter 8 - Visual Basic Schneider 1 Chapter 3-B Files and Functions

Run-time errors

Errors that occur while a program is running are called run-time errors•Division by zero

•File not found

Page 50: Chapter 8 - Visual Basic Schneider 1 Chapter 3-B Files and Functions

Example

Page 51: Chapter 8 - Visual Basic Schneider 1 Chapter 3-B Files and Functions

Logical errors

Occurs when a program dose not perform the way it was intended

A program with a logic error is a valid program in the language, though it does not behave as intended. •Average = num1 + num2 / 2

Page 52: Chapter 8 - Visual Basic Schneider 1 Chapter 3-B Files and Functions

Debugging

The process of finding and correcting errors

Page 53: Chapter 8 - Visual Basic Schneider 1 Chapter 3-B Files and Functions

Reference

Chapter 3 - Visual Basic - Schneider