Download presentation
Presentation is loading. Please wait.
Published byCarole Petit Modified over 6 years ago
1
Section 3.3 Numbers Arithmetic Operations Variables
Incrementing the Value of a Variable Built-In Functions: Math.Sqrt Int Math.Round
2
Numbers continued The Integer Data Type Multiple Declarations
Parentheses Three Types of Errors
3
Arithmetic Operations
Numbers are called numeric literals Five arithmetic operations in VB.NET + addition - subtraction * multiplication / division ^ exponentiation Think about Precedence !
4
Variables In VB.Net variable name can be up to
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
5
Variables Declaration: Assignment: Dim speed As Double speed = 50
Data type Variable name Assignment: speed = 50
6
Variables Declaration: Dim staffMembers As Integer
Integer Variable can take Whole numbers from -2 billion to + 2 billion
7
Variable Declaration Dim speed As Double
Dim a As Integer (Used mainly for counting) Dim b As String
8
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.
9
Display Numeric Literals
lstResults.Items.Add(1.2 * 10 ^34) 1.2 E + 34 lstResults.Items.Add(n+v)
10
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.)
11
Incrementing To add 1 to the numeric variable var Or as a shortcut
var = var + 1 Or as a shortcut var +=1
12
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
13
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
14
Three Types of Errors Syntax error Run-time error Logic error
15
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
16
Comments 1/0 will give infinity 1/infinity will give 0 As answer
When n is 2.5, 1.5, etc then it rounds to the nearest even number Math.Round(2.5) = 2 Math.Round(3.5) = 4
17
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
18
Strings continued The Empty String Initial Value of a String
Option Strict Internal Documentation Line-Continuation Character
19
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
20
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”
21
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
22
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
23
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
24
ANSI Character Set A numeric representation for every key on the keyboard
25
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
26
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.
27
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
28
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 = ""
29
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"
30
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
31
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.
32
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)"
33
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
34
Formatting Output with Format Functions
String Value FormatNumber( ,1) 12,345.6 FormatCurrency( ,2) $12,345.63 FormatPercent(0.185,2) 18.50%
35
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))
36
Inputting Data Data can be stored in files and accessed with a StreamReader object or supplied by the user with an input dialog box.
37
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()
38
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")
39
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")
40
Have Fun! V.B. is Enjoyable, especially with Visual Studio.Net
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.