Section 3.3 Numbers Arithmetic Operations Variables Incrementing the Value of a Variable Built-In Functions: Math.Sqrt Int Math.Round
Numbers continued The Integer Data Type Multiple Declarations Parentheses Three Types of Errors
Arithmetic Operations Numbers are called numeric literals Five arithmetic operations in VB.NET + addition - subtraction * multiplication / division ^ exponentiation Think about Precedence !
Variables In VB.Net variable name can be up to 16 383 characters long Must Begin with a Letter or _Underscore Can Consists only of Letter, Numbers, Underscore Not Case Sensitive Convention is to write them in Lower case except 1st letters of additional words
Variables Declaration: Assignment: Dim speed As Double speed = 50 Data type Variable name Assignment: speed = 50
Variables Declaration: Dim staffMembers As Integer Integer Variable can take Whole numbers from -2 billion to + 2 billion
Variable Declaration Dim speed As Double Dim a As Integer (Used mainly for counting) Dim b As String
Display Numeric Literals lstBox.Items.Add(n) Use lstBox.Items.Clear() to remove all the current content of the list box Here Add is a Method – a process that performs a task for a particular object. If ( ) contain (Numeric Expression) then it first evaluates the expression.
Display Numeric Literals lstResults.Items.Add(1.2 * 10 ^34) 1.2 E + 34 lstResults.Items.Add(n+v)
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.)
Incrementing To add 1 to the numeric variable var Or as a shortcut var = var + 1 Or as a shortcut var +=1
Multiple Declarations Dim a, b As Double Two other types of multiple-declaration statements are Dim a As Double, b As Integer Dim c As Double = 2, b As Integer = 5
Functions Math.Sqrt(9) is 3 Int(2.7) is 2 Int(2) is 2 Int(-2.7) is –3 Math.Round(2.7) is 3 Math.Round(2.317, 2) is 2.32
Three Types of Errors Syntax error Run-time error Logic error
Comments Declaring variables at the beginning of event procedure is a good habit Keywords can’t be used as names of variables Names given to variables are also called identifiers What will be the out put for Math.Sqrt(-1) NaN
Comments 1/0 will give infinity 1/infinity will give 0 As answer When n is 2.5, 1.5, 3.5 4.5 etc then it rounds to the nearest even number Math.Round(2.5) = 2 Math.Round(3.5) = 4
3.4 Strings Variables and Strings Using Text Boxes for Input and Output Concatenation ANSI Character Set String Properties and Methods: Length ToUpper Trim ToLower IndexOf Substring
Strings continued The Empty String Initial Value of a String Option Strict Internal Documentation Line-Continuation Character
Let’s talk about Strings A string literal is a sequence of characters that is treated as a single item. Strings are combined by concatenation Eg. str1 & str2 “Santosh”& “ Panchal” = Santosh Panchal A string variable is a name used to refer to a string. Allowable names are same as numeric Var
Variables and Strings Private Sub btnDisplay_Click(...) Handles btnDisplay.Click Dim today As String today = "Monday" With lstOutput.Items .Clear() .Add("hello") .Add(today) End With End Sub Pay Attention to “With”
Using Text Boxes for Input and Output The contents of a text box is always a string Input example strVar = txtBox.Text Output example txtBox.Text = strVar
Data Conversion Because the contents of a text box is always a string, sometimes you must convert the input or output(type casting) numVar = CDbl(txtBox.Text) txtBox.Text = CStr(numVar) Converts a String to a Double Converts a number to a string
Concatenation Combining two strings to make a new string This is a String expression quote1 = "The ballgame isn't over, " quote2 = "until it's over." quote = quote1 & quote2 txtOutput.Text = quote & " Yogi Berra" Displays The ball game isn't over until it's over. Yogi Berra
ANSI Character Set A numeric representation for every key on the keyboard
ddd Chr(n) will display a string with ANSI value n. Asc(str) will display the ANSI value of the first character of str. Eg. txt.Box.Text = Chr(65) A lstBox.Items.Add(Asc(“Apple”)) 65 txtBox.Text = “32”&Chr(176)& “Fahrenheit” 32o Fahrenheit
String Properties and Methods: Imp. - A string is an OBJECT "Visual".Length is 6. "Visual".ToUpper is VISUAL. "123 Hike".Length is 8. "123 Hike".ToLower is 123 hike. "a" & " bcd ".Trim & "efg" is abcdefg.
More String Properties and Methods: "fanatic".Substring(0, 3) is "fan". "fanatic".IndexOf("ati") is 3. "fanatic".Substring(4, 2) is "ti". "fanatic".IndexOf("a") is 1. "fanatic".Substring(4) is "tic". "fanatic".IndexOf("nt") is –1. Let’s Do Ex. 6 – Page 97
The Empty String The string "", which contains no characters, is called the empty string or the zero-length string. The statement lstBox.Items.Add("") skips a line in the list box. The contents of a text box can be cleared with either the statement txtBox.Clear() or the statement txtBox.Text = ""
Initial Value of a String By default the initial value is Nothing Strings can be given a different initial value as follows: Dim today As String = "Monday"
Option Strict VB.NET allows numeric variables to be assigned strings and vice versa, a poor programming practice. To turn this feature off, put the following statement at the very top of the code window Option Strict On
Internal Documentation Other people can easily understand the program. You can understand the program when you read it later. Long programs are easier to read because the purposes of individual pieces can be determined at a glance.
Line-Continuation Character A long line of code can be continued on another line by using underscore (_) preceded by a space msg = "640K ought to be enough " & _ "for anybody. (Bill Gates, 1981)"
3.5 Input and Output Formatting Output with Format Functions Formatting Output with Zones Reading Data from Files Getting Input from an Input Dialog Box Using a Message Dialog Box for Output
Formatting Output with Format Functions String Value FormatNumber(12345.628,1) 12,345.6 FormatCurrency(12345.628,2) $12,345.63 FormatPercent(0.185,2) 18.50%
Formatting Output with Zones Use a fixed-width font such as Courier New Divide the characters into zones with a format string. Dim fmtStr As String = "{0, 15}{1, 10}{2, 8}" lstOutput.Items.Add(String.Format(fmtStr, data0, data1, data2))
Inputting Data Data can be stored in files and accessed with a StreamReader object or supplied by the user with an input dialog box.
Steps to Use StreamReader Execute a statement of the form Dim readerVar As IO.StreamReader = _ IO.File.OpenText(filespec) or the pair of statements Dim readerVar As IO.StreamReader readerVar = IO.File.OpenText(filespec) Assume the file contains one item of data per line. Read items of data in order, one at a time, from the file with the ReadLine method. strVar = readerVar.ReadLine After the desired items have been read from the file, terminate the communications link readerVar.Close() 1. Execute a statement of the form Dim readerVar As IO.StreamReader A StreamReader is an object from the Input/Output class that can read a stream of characters coming from a disk or coming over the Internet. The Dim statement declares the variable readerVar to be of type StreamReader. 2. Execute a statement of the form readerVar = IO.File.OpenText(filespec) where filespec identifies the file to be read. This statement establishes a communi-cations link between the computer and the disk drive for reading data from the disk. Data then can be input from the specified file and assigned to variables in the pro-gram. This assignment statement is said to “open the file for input.” Just as with other variables, the declaration and assignment statements in Steps 2 and 3 can be combined into the single statement Dim readerVar As IO.StreamReader = IO.File.OpenText(filespec) 3. Read items of data in order, one at a time, from the file with the ReadLine method. Each datum is retrieved as a string. A statement of the form strVar = readerVar.ReadLine causes the program to look in the file for the next unread line of data and assign it to the variable strVar. The data can be assigned to a numeric variable if it is first converted to a numeric type with a statement such as numVar = CDbl(readerVar.ReadLine) Note: If all the data in a file have been read by ReadLine statements and another item is requested by a ReadLine statement, the item retrieved will have the value Nothing. 4. After the desired items have been read from the file, terminate the communications link set in Step 3 with the statement readerVar.Close()
Getting Input from an Input Dialog Box stringVar = InputBox(prompt, title) fileName = InputBox("Enter the name " _ & "of the file containing the " & _ "information.", "Name of File")
Using a Message Dialog Box for Output MsgBox(prompt, , title) MsgBox("Nice try, but no cigar.", , "Consolation") 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")
Have Fun! V.B. is Enjoyable, especially with Visual Studio.Net