Variable Review & IO User 12/26/2018.

Slides:



Advertisements
Similar presentations
Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
Advertisements

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:
IS 1181 IS 118 Introduction to Development Tools VB Chapter 06.
Chapter 8 - VB.Net by Schneider1 Chapter 8 – Sequential Files 8.1 Sequential Files Creating a Sequential File Adding Items to a Sequential File Error Trapping.
Variables & Math Operators CE 311 K - Introduction to Computer Methods Daene C. McKinney.
Chapter 3 - VB 2008 by Schneider1 Chapter 3 – Variables, Input, and Output 3.1 Numbers 3.2 Strings 3.3 Input and Output.
Chapter 31 Fundamentals of Programming in Visual Basic (Continue VI) String Properties and Methods: "Visual".Length is 6. "Visual".ToUpper is VISUAL. "123.
1 Chapter 3 – Variables, Input, and Output 3.1 Numbers 3.2 Strings 3.3 Input and Output.
Created By Mayson Al-Duwais1. Using Exit to Terminate Repetition Statements To terminate different types of repetition statements you can use a special.
Input/Output CE 311 K - Introduction to Computer Methods Daene C. McKinney.
CS0004: Introduction to Programming Input and Output.
Chapter 3 - VB.NET by Schneider1 Chapter 3 – Fundamentals of Programming in VB.NET VB.NET Controls VB.NET Events Numbers Strings Input and Output.
CS0004: Introduction to Programming Variables – Strings.
Chapter 8 - VB 2008 by Schneider1 Chapter 8 – Sequential Files 8.1 Sequential Files 8.2 Using Sequential Files.
1 Chapter 3 – Variables, Input, and Output 3.1 Numbers 3.2 Strings 3.3 Input and Output.
1 Chapter 3 – Variables, Input, and Output 3.1 Numbers 3.2 Strings 3.3 Input and Output.
Chapter 3 - VB 2008 by Schneider1 Chapter 3 – Variables, Input, and Output 3.1 Numbers 3.2 Strings 3.3 Input and Output.
Creating Sequential Files, Adding Data, & Deleting Data.
Microsoft Visual Basic 2005 CHAPTER 4 Variables and Arithmetic Operations.
Input Textboxes Input Boxes Different than textboxes Good for small amount of input (form full of textboxes is not nice) X = Inputbox(“prompt message”,
Reference: Lecturer Lecturer Reham O. Al-Abdul Jabba lectures for cap211 Files and Streams- I.
1 COMP3100e Developing Microsoft.Net Applications for Windows (Visual Basic.Net) Class 6 COMP3100E.
1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.
CS0004: Introduction to Programming Project 1 – Lessons Learned.
Numbers continued The Integer Data Type Multiple Declarations Parentheses Three Types of Errors.
Chapter 8 - Visual Basic Schneider
Variables & Function Calls. Overview u Variables  Programmer Defined & Intrinsic  Data Types  Calculation issues u Using Functions  The val() function.
1 Chapter 3 – Examples The examples from chapter 3, combining the data types, variables, expressions, assignments, functions and methods with Windows controls.
Hungarian Notation A must in this course Every object used MUST be renamed including the form(s) using the following rules Form  frmFormName E.g. frmTemperature.
BACS 287 File-Based Programming. BACS 287 Data Hierarchy  Database - Collection of files, relationships, integrity information, etc  Files - All records.
CS4 –lecture 6 Wednesday, Jan 19, 2011 Roxana Gheorghiu.
Variables and Expressions Programming Right from the Start with Visual Basic.NET 1/e 7.
Microsoft Visual Basic 2012 CHAPTER FOUR Variables and Arithmetic Operations.
3.4 Strings Variables and Strings Using Text Boxes for Input and Output Concatenation ANSI Character Set String Properties and Methods: LengthToUpper TrimToLower.
Chapter 3 - VB 2008 by Schneider1 Chapter 3 – Variables, Input, and Output 3.1 Numbers 3.2 Strings 3.3 Input and Output.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Data Types. Visual Basic provides data type Single for storing single-precision floating-point numbers. Data type Double requires more memory to store.
Microsoft Visual Basic 2010 CHAPTER FOUR Variables and Arithmetic Operations.
Visual Basic Fundamental Concepts
A variable is a name for a value stored in memory.
Chapter 3 – Fundamentals of Programming in Visual Basic
Chapter 3 – Variables, Input, and Output
Apply Procedures to Develop Message, Input, and Dialog Boxes
Data Types, Arithmetic Operations
Variables and Arithmetic Operations
Introduction to Scripting
Chapter 3 – Variables, Input, and Output
Chapter 8 – Sequential Files
Variables and Arithmetic Operations
Visual Basic..
Topics Introduction to File Input and Output
WEB PROGRAMMING JavaScript.
Chapter 3 – Variables, Input, and Output
Fundamentals of Programming in VB.NET
Numbers.
Strings(Part 1) String Literal and String Variable
String Variable, Methods and Properties
Section 3.3 Numbers Arithmetic Operations Variables
Chapter 3.5 Input and Output
Input and Output.
String Variable, Methods and Properties
Additional Topics in VB.NET
CIS16 Application Development and Programming using Visual Basic.net
String Variable, Methods and Properties
Introduction to Programming
Topics Introduction to File Input and Output
String Variable, Methods and Properties
Input and Output.
Topics Introduction to File Input and Output
Input and Output Chapter 3.5
Presentation transcript:

Variable Review & IO User 12/26/2018

Introduction Review variable typing and dimension statement format Data type conversion File IO 12/26/2018

Variable Initialization Numeric variables are automatically initialized to 0: Dim varName As Double To specify a nonzero initial value Dim varName As Double = 50 declares a variable named varName to be of type Double. Actually, the Dim statement causes the computer to set aside a location in memory with the name varName. Since varName is a numeric variable, the Dim statement also places the number zero in that memory location. (We say that zero is the initial value or default value of the variable.) 12/26/2018

Multiple Declarations multiple-declaration statements : Dim a, b As Double Dim a As Double, b As Integer Dim c As Double = 2, b As Integer = 5 12/26/2018

Data Conversion Type-casting functions Converts data from one type to another such as CDbl(n), Cint(n), CStr(n) Previously we used intrinsic functions to convert numeric to string and string to numeric Val(n), Str(n) 12/26/2018

Data Conversion numVar = CDbl(txtBox.Text) txtBox.Text = CStr(numVar) Converts a String to a Double Converts a number to a string CType(numVar,string) ‘Alternative conversion methods Convert.ToString(numVar) numVar.ToString Str(numVar) 12/26/2018

CType() CType(expression, typename) expression – any variable or string literal expression typename – any VB datatype, object, class or structure allowed with in the VB framework Data Conversion numVar = CDbl(txtBox.Text) txtBox.Text = CStr(numVar) 12/26/2018

ANSI Character Set If n is between 32 and 255, str Chr(n):returns the string consisting of the character with ANSI value n Chr(65) ----- A Asc(str):returns the ANSI value of the first character of str Asc(“Apple”) --- 65 32 & Chr(176) & “Fahrenheit -- 32° Fahrenheit 12/26/2018

Strings String literal a sequence of characters treated as a single item, surrounded by “” String variable a name used to refer to a string Dim varName As String 12/26/2018

String Properties and Methods A string is an object, like controls, has both properties and methods. Length ToUpper Trim ToLower IndexOf Substring 12/26/2018

String Properties str.Length: the number of characters in str Where str is any string literal or attribute the number of characters in str “Tacoma".Length is 5 12/26/2018

String methods str.ToUpper format str.ToLower with all letters of str capitalized format “Tacoma".ToUpper is TACOMA. str.ToLower with all letters of str in lowercase format “TCC1926".ToLower is tcc1926 12/26/2018

Concatenation Combining two strings into a new string.Concatenation is represented by “&” Dim str1 As String = “My GPA is A " Dim Str2 As String = “in Winter 2002" txtOutput.Text = str1 & str2 displays My GPA is A in Winter 2002 12/26/2018

Concatenation Combining strings with numbers into a string Dim str1 As String = “My grade is “ Dim grade As Double = 89 Dim str2 As String = “in Winter 2002" txtOutput.Text = str1 & grade & str2 displays My GPA is 89 in Winter 2002 12/26/2018

String Methods str.Substring(m,n) str.Substring(m) substring consisting of n characters beginning with the character in position m in str “Washington”.Substring(0,4) is “Wash” str.Substring(m) substring beginning with the character in position m in str until the end of str “Washington”.Substring(2) is “shington” 12/26/2018

The Empty String zero-length string "" lstBox.Items.Add("") skips a line txtBox.Text = "“ or txtBox.Clear() clear the text box 12/26/2018

Initial Value of a String A string should be assigned a value before being used By default the initial value is Nothing Strings can be given a different initial value as follows: Dim today As String = "Monday“ 12/26/2018

Option Strict The Option Strict statement must appear before any other code Restricts implicit data type conversions to only widening conversions Provides compile-time notification of data lost conversions Generates an error for any undeclared variable 12/26/2018

Option Strict The default is Off Option Strict { On | Off } On: Optional. Enables Option Strict checking Off : Optional. Disables Option Strict checking 12/26/2018

Option Strict Example Option Strict On Dim MyVar As Integer Dim Obj As Object MyVar = 1000 MyVar = 1234567890.987654321 ‘ Data lost MyInt = 10 ' Undeclared variable 12/26/2018

Using Text Boxes for Input and Output The contents of a text box is a string The Property “Text” can only be assigned to a string variable type Assigning input strVar = txtBox.Text When using the textbox for output Assigning Output set(ReadOnly = true) txtBox.Text = strVar 12/26/2018

Data Files and IO Streams 12/26/2018

Creating a Sequential File Choose a filename – may contain up to 215 characters Select the path for the folder to contain this file Execute a statement like the following: Dim sw As IO.StreamWriter = IO.File.CreateText(filespec) 12/26/2018

Open Modes OpenText Format of the IO statement to read data: Dim sr As IO.StreamReader = IO.File.OpenText(“Colleges.txt") The argument “sr” is used here to indicate a read string CreateText Format of the IO statement to write data: Dim sw As IO.StreamWriter = IO.File.CreateText(“Colleges.txt") The argument “sw” is used here to indicate a write string AppendText Format of the IO statement to Add data: Dim sw As IO.StreamWriter = IO.File.AppendText(“Colleges.txt") 12/26/2018

Open Modes CreateText = Opens the file and clears the file, it writes only new data to the file OpenText = Opens the file so that the records can be read AppendText = Opens the file and writes new records to the end of the file 12/26/2018

Adding Items to a Sequential File Execute the statement Dim sw As IO.StreamWriter = IO.File.AppendText(filespec) where sw is a variable name used to store the data in memory and filespec identifies the file. Place data into the file with the WriteLine method. After all the data have been recorded into the file, close the file with the statement sw.Close() 12/26/2018

Creating a Sequential File… Place a line of data into the file with a statement of the form: sw.WriteLine(attribute) Close the file when all data has been added: sw.Close() 12/26/2018

Example 1 Private Sub btnCreateFile_Click(...) _ Handles btnCreateFile.Click Dim sw As IO.StreamWriter = IO.File.CreateText(“Colleges.TXT") With sw .WriteLine(“Missouri Western") .WriteLine(“5475") .WriteLine(strCollege) .WriteLine(strFaculty) .Close() End With End Sub 12/26/2018

Caution If an existing file is opened for output, the computer will erase the existing file and create a new one. What open mode will clear the file? What open mode will add to the file? 12/26/2018

Adding Items to a Sequential File Execute the statement Dim sw As IO.StreamWriter = IO.File.AppendText(filespec) where sw is a variable name to store the date in memory and filespec identifies the file. Write to the file with the WriteLine method. After all the data have been recorded close. sw.Close() 12/26/2018

IO.File.AppendText Append adds to the end of the file If a file does not exist, it will create one 12/26/2018

Sequential File Modes CreateText OpenText AppendText A file should not be opened in two different modes at the same time. 12/26/2018

Code to Read from the file ' Read data from a file and display in a list box Private Sub btnRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRead.Click ' Establish a communication link Dim sr As IO.StreamReader = IO.File.OpenText("Grades.txt") ' Read data line by line and display them in listbox lstDisplay.Items.Clear() lstDisplay.Items.Add(sr.ReadLine()) ' Terminate the communication link sr.Close() End Sub 12/26/2018

Input Dialog Box strVar = InputBox(prompt, title) 12/26/2018

Code for Input box ' Read data from a file ans display in a list box Private Sub btnRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRead.Click Dim fileName As String ‘set an argument for the file name Dim sr As IO.StreamReader ‘set an argument as the read string ' Get the file name from user fileName = InputBox("Enter the file name", "Enter file name") ' establish a communication link sr = IO.File.OpenText(fileName) ' Read data line by line, display them and the average lstDisplay.Items.Clear() lstDisplay.Items.Add(sr.ReadLine()) ' Terminate the communication link sr.Close() End Sub 12/26/2018

Format Functions FormatNumber(n,r) FormatCurrency(n,r) FormatPercent(n,r) return a string value n - a number, an numeric expression or a string to be formatted r - the number of decimal places default value is 2 12/26/2018

Formatting Output with Format Functions String Value FormatNumber(12345.628,1) FormatNumber(1 + 2) 12,345.6 3.00 FormatCurrency(12345.628,2) FormatCurrency(-100) $12,345.63 ($100.00) FormatPercent(0.185,2) FormatPercent(“0.07”) 18.50% 7.00% 12/26/2018

Code the Output ' Format data using format functions Private Sub btnFormat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFormat.Click lstDisplay.Items.Add(strCollege) lstDisplay.Items.Add(FormatNumber(lngFaculty)) lstDisplay.Items.Add(FormatNumber(lngStudents)) lstDisplay.Items.Add(FormatPercent(sngRatio,2)) End Sub 12/26/2018

Formatting Output with Zones Line up items in columns Use a fixed-width font such as Courier New Divide the characters into zones with a format string. 12/26/2018

Formatting output with zones Zone width Dim fmtStr As String = "{0, 15}{1, 10}{2, 8}“ lstOutput.Items.Add(String.Format(fmtStr, _ data0, data1, data2)) Zone number 12/26/2018

Numbers with FMT-zones A colon and formatting symbol after width to specially format numeric data Zone Format Term Data Number displayed {1,12:N3} 1234.5679 1234.568 {1,12:N0} 34.6 34 {1,12:C1} 1234.567 $1234.6 {1,-12:P} 0.569 56.90% 12/26/2018

Formatting with Zones Zone width left adjusted if preceded with minus sign, right adjusted otherwise Spaces between the successive pairs of brackets will be displayed in the corresponding zones in the output. 12/26/2018

Code Example 12/26/2018

Dim sr As IO.StreamReader Dim fmtStr As String = "{0,-15}{1,15}{2,15}" ' Read data from a file ans display in a list box Private Sub btnRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRead.Click Dim fileName As String Dim sr As IO.StreamReader Dim fmtStr As String = "{0,-15}{1,15}{2,15}" ' Get the file name from user fileName = InputBox("Enter file name", “File name") ' Establish a communication link sr = IO.File.OpenText(fileName) ' Read data line by line, display them and the average lstDisplay.Items.Clear() lstDisplay.Items.Add(String.Format(fmtStr, “City", “Population", “Jobs")) lstDisplay.Items.Add(String.Format(fmtStr, sr.ReadLine(), sr.ReadLine(), sr.ReadLine())) ' Terminate the communication link sr.Close() End Sub 12/26/2018

Using a Message Dialog Box for Output MsgBox(prompt,title) MsgBox(prompt, , title) is executed, where prompt and title are strings, a message dialog box appears with prompt displayed and the title bar caption title and stays on the screen until the user presses Enter, clicks on the box in the upper-right corner, or clicks OK. For instance, the state-ment MsgBox("Nice try, but no cigar.", , "Consolation") 12/26/2018

Code 'Message sent before clearing the items in a list box Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click MsgBox(" All items will be deleted", , "Delete items") lstDisplay.Items.Clear() End Sub 12/26/2018