Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 8 - Visual Basic Schneider

Similar presentations


Presentation on theme: "Chapter 8 - Visual Basic Schneider"— Presentation transcript:

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

2 Reading Data from Files
1. Choose a number to be the reference number for the file 2. Execute an Open statement 3. Read the data sequentially using Input # statements 4. Close the file 1. The name of the file: follows the same rules for naming program files. Use invest.bas and invest.dat 2. The mode in which the file is to be used , can be output, input, append . Output: indicates data is written to the file from the program input: the contents of the file are being read into the program append :allows new records to be added to an existing file. A butter is a reserved part of the primary storage unit used a s temporary storage area for the data that is being written or read from a file Chapter 3 - Visual Basic Schneider hhhhhhh

3 Example of Reading from a File:
Open the file Open “DATA.TXT” For Input As #1 Input #1, num1 Input #1, num2 picOutput.Print num1+num2  7 Close #1 Reference number Read the data and assign it to num1 Data.txt A file can have either one item per line or many items (separated by commas) can be listed on the same line the items of data will be assigned to variables one at the time in the order they appear in the file. 1. Choose a number from 1 to 255 to be the reference number for the file 2. Open the file for input 3. Read items of data in order, one at the time, from the file with Input sattement Show Examples: Path is C;\F98 CSE181\Examples\Ch3\Data.txt The response typed into an input box is treated as a single string value, no matter what is typed. Quotation marks are not needed, and if included, are considered as part of the string. Numeric data typed into n input box should be converted to a number with Val before it is assigned to a numeric variable or used in calculation. Close the file 3 4 Chapter 3 - Visual Basic Schneider hhhhhhh

4 Example of Reading from a File:
Data.txt Open “DATA.TXT” For Input As #1 Input #1, num1, num2 picOutput.Print num1+num2  7 Close #1 3 4 math.txt 2 4 5 8 5 6 Example #1: create a file named Student.dat Prepare the file to receive data If a file already exists, it is destroyed and a new file is created. Associated with the file Student.date with buffer #1. As long as the file is open. Buffer #1 is used temporarily before the data is written to the file on disk Other statement in the program use this number to identify the file.. Chapter 3 - Visual Basic Schneider hhhhhhh

5 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. Chapter 3 - Visual Basic Schneider hhhhhhh

6 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 After users types in a response into the rectangle at the bottom of the screen and press Enter, the response is assigned to the string variable. Normally, text box is used to obtain input, sometimes we want just one piece of input and would rather not have a text box and label stay on the screen for ever. Example Chapter 3 - Visual Basic Schneider hhhhhhh

7 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 Chapter 3 - Visual Basic Schneider hhhhhhh

8 Example of a Message Dialog Box
MsgBox "CS116", , "Visual Basic" Stays on the screen until the user presses OK hhhhhhh 8

9 Example of a Message Dialog Box
MsgBox "CS116"

10 Example

11 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 Chapter 3 - Visual Basic Schneider hhhhhhh

12 Chapter 3 - Visual Basic Schneider
Semicolons The next value output is placed in the next column position. Example: picOutput.Print “Patrick”; ”Jon” Output: PatrickJon Chapter 3 - Visual Basic Schneider hhhhhhh

13 Chapter 3 - Visual Basic Schneider
Example of Semicolon picOutput.Print “Patrick”; “ Jon” Output Screen: Patrick Jon Space here Space here Chapter 3 - Visual Basic Schneider hhhhhhh

14 Chapter 3 - Visual Basic Schneider
Example of Semicolon picOutput.Print 100; -200; 300 Output Screen: Two spaces One space Chapter 3 - Visual Basic Schneider hhhhhhh

15 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. Chapter 3 - Visual Basic Schneider hhhhhhh

16 Chapter 3 - Visual Basic Schneider
Using Commas Example: picOutput.Print “SEE”, ”YOU”, ”SOON” Output Screen: SEE YOU SOON See in column 1 Y in column 15 and S in column 29 Column 29 Column 15 Column 1 Chapter 3 - Visual Basic Schneider hhhhhhh

17 Chapter 3 - Visual Basic Schneider
Using Commas Example: picOutput.Print “abc123def456ghi”, ”whatever” Output Screen: abc123def456ghi whatever See in column 1 Y in column 15 and S in column 29 Column 29 Column 15 Column 1 Chapter 3 - Visual Basic Schneider hhhhhhh

18 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 Example 3.5.5 Column 29 Chapter 3 - Visual Basic Schneider hhhhhhh

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

20 Private Sub cmdAdd_Click()
picoutput.Print " " picoutput.Print 10; 20; picoutput.Print -10, 30, picoutput.Print picoutput.Print 15; ; 6 picoutput.Print "Hello"; "hi"; picoutput.Print "12345", "12" End Sub ;  in the next position column position ,  in the next available print zone picOutput.Print  moves the cursor to the beginning of the next line

21 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) Chapter 3 - Visual Basic Schneider hhhhhhh

22 Example of Tab Function
picOutput.Print Tab(3); “Hi there!” ; Tab(25) ;“Bye!” Output Screen: Hi there! Bye! Example 3.5.6 Organizes data into columns of a table Column 25 Column 3 Chapter 3 - Visual Basic Schneider hhhhhhh

23 Example of Tab Function
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! Example 3.5.6 Organizes data into columns of a table Column 3 Column 5 Chapter 3 - Visual Basic Schneider hhhhhhh

24 Tab

25 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) Chapter 3 - Visual Basic Schneider hhhhhhh

26 Chapter 3 - Visual Basic Schneider
Numeric Functions Example for Int function and Sqr function Example : rounds a positive number to the nearest integer.. To round a number t two decimal places r = Int (100*n + 0.5) / 100 Chapter 3 - Visual Basic Schneider hhhhhhh

27 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: Chapter 3 - Visual Basic Schneider hhhhhhh

28

29 Something about Round with number 5
123.8 12.4 4 4 5 23.6 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

30 Commonly-Used String Functions
Function: Left(“Penguin”, 4) Purpose: Returns the number of characters specified, starting at the beginning of the string Peng Chapter 3 - Visual Basic Schneider hhhhhhh

31 Commonly-Used String Functions
Function: Right(“Gotham City”, 4) Purpose: Returns the number of characters specified from the end of the string City Chapter 3 - Visual Basic Schneider hhhhhhh

32 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 Chapter 3 - Visual Basic Schneider hhhhhhh

33 Commonly-Used String Functions
Function: UCase(“Yes”) Purpose: Converts any lowercase letters in a string to uppercase YES Go over Example 6 page 121 Chapter 3 - Visual Basic Schneider hhhhhhh

34 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 Chapter 3 - Visual Basic Schneider hhhhhhh

35 String-Related Numeric Function
Function: Len(“John Smith”) Purpose: Returns the number of characters in the string. 10 Is 10 Chapter 3 - Visual Basic Schneider hhhhhhh

36 Strings and string Functions examples

37 Example

38 Example

39 Chapter 3 - Visual Basic Schneider
Format Functions The format functions provide detailed control of how numbers, dates, and strings are displayed. Examples FormatNumber ( , 1) ,345.7 FormatCurrency ( , 2) $12,345.68 FormatPercent (.185, 2) % FormatNumber (1 + Sqr(2), 3) Numbers can be made to line up uniformly and be displayed with dollar signs, commas and a specified number of decimal places. Dates can be converted to a long or medium form. Strings can be right justified. Chapter 3 - Visual Basic Schneider hhhhhhh

40 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). picBox.Print Rnd +5 Output: Displays a random number from 5 to 6 (5 included and 6 excluded). Chapter 3 - Visual Basic Schneider hhhhhhh

41 Chapter 3 - Visual Basic Schneider
Rnd Function Example: picBox.Print Int(Rnd) Output: Displays 0. picBox.Print Int(Rnd +5) Output: Displays 5. picBox.Print Int(Rnd) +5 Chapter 3 - Visual Basic Schneider hhhhhhh

42 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) picBox.Print Int(5*Rnd) +2 Output: Displays a random Integer from 2 to 6 (2 and 6 included). Chapter 3 - Visual Basic Schneider hhhhhhh

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

44 Exercises: Position the location of the form at run time  Form layout windows To open a file called “data.txt” located in C:\VB folder you must write  Open “C:\VB\data.txt” for input as #1 Which of the following statement assign the content of the input box to a numeric variable X X = Val(InputBox(“Enter the number”))

45 What is the output of the following code?
Dim s as integer, n as integer Open “data.txt” for Input as #1 Input #1,n s=s+n close #1 print “s=”;s output: s= 60 data.txt 10 20 30

46 what is output of the following code?
Dim n as integer Open “data.txt” for Input as #1 Input #1,n Close #1 Print “n = ”; n Output: n = 20 data.txt 10 20 30

47 what is output of the following code?
Dim n as integer Open “data.txt” for Input as #1 Input #1,n Close #1 Print “n = ”; n Output: n = 10 data.txt 10 20 30

48 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+1 End If

49 Run-time errors Errors that occur while a program is running are called run-time errors Division by zero File not found

50 Example

51 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

52 Debugging The process of finding and correcting errors

53 Chapter 3 - Visual Basic - Schneider
Reference Chapter 3 - Visual Basic - Schneider


Download ppt "Chapter 8 - Visual Basic Schneider"

Similar presentations


Ads by Google