Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fundamentals of Programming in VB.NET

Similar presentations


Presentation on theme: "Fundamentals of Programming in VB.NET"— Presentation transcript:

1 Fundamentals of Programming in VB.NET
Chapter 3 Fundamentals of Programming in VB.NET 11/30/2018 Fundamentals of Programming in VB.Net

2 Fundamentals of Programming in VB.Net
VB.NET Controls 11/30/2018 Fundamentals of Programming in VB.Net

3 Fundamentals of Programming in VB.Net
Add an "access key" 11/30/2018 Fundamentals of Programming in VB.Net

4 Fundamentals of Programming in VB.Net
The Name Property How the programmer refers to a control in code Name must begin with a letter Must be less than 215 characters long May include numbers and the underscore Use appropriate 3 character naming prefix 11/30/2018 Fundamentals of Programming in VB.Net

5 Fundamentals of Programming in VB.Net
Control Name Prefixes 11/30/2018 Fundamentals of Programming in VB.Net

6 Fundamentals of Programming in VB.Net
VB.Net Events 11/30/2018 Fundamentals of Programming in VB.Net

7 Fundamentals of Programming in VB.Net
VB.NET Events An Event Procedure Walkthrough Properties and Event Procedures of the Form The Declaration Statement of an Event Procedure 11/30/2018 Fundamentals of Programming in VB.Net

8 Fundamentals of Programming in VB.Net
What is an event? An event is an action, such as the user clicking on a button Usually, nothing happens until the user does something and generates an event 11/30/2018 Fundamentals of Programming in VB.Net

9 Fundamentals of Programming in VB.Net
VB.NET program: Create the interface; that is, generate, position, and size the objects. Set properties; that is, configure the appearance of the objects. Write the code that executes when events occur. This block of code is called event procedure. 11/30/2018 Fundamentals of Programming in VB.Net

10 Fundamentals of Programming in VB.Net
Event Procedures Private Sub objectName_event( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles objectName.event statements End Sub Private Sub ObjectName_event(…) Handles objectName.event Because the book does not make use of the parameter list, it is replaced with an ellipsis 11/30/2018 Fundamentals of Programming in VB.Net

11 Fundamentals of Programming in VB.Net
Statements Properties of controls can be changed both in the Properties window and in the statements of the event procedure. The statement we use to change the property value is call an Assignment statement 11/30/2018 Fundamentals of Programming in VB.Net

12 Fundamentals of Programming in VB.Net
Changing Properties Properties are changed in code with the following: controlName.property = setting This is an assignment statement txtBox.ForeColor = Color.Red Me.Text = "Demonstration“ Instead of (VB.6) Form1.Text = "Demonstration" The form is referred to by the keyword Me. The setting value on the right hand side of the equal sign is being assigned to the property of the control listed on the left hand side of the equal sign 11/30/2018 Fundamentals of Programming in VB.Net

13 Fundamentals of Programming in VB.Net
Program Region 11/30/2018 Fundamentals of Programming in VB.Net

14 Dropdown Boxes Automatically pops up to give the programmer help.
11/30/2018 Fundamentals of Programming in VB.Net

15 Fundamentals of Programming in VB.Net
Help Help menu Press ALT/H/I - Help index window Dynamic help – samples (Member Listing) Type dot “.” following a word Type parenthesis following a word Context-sensitive help Place cursor on the word, and press F1 11/30/2018 Fundamentals of Programming in VB.Net

16 Fundamentals of Programming in VB.Net
Event Outcomes Private Sub txtFirst_TextChanged(...) Handles txtFirst.TextChanged txtFirst.ForeColor = Color.Blue End Sub Private Sub btnRed_Click(...) Handles btnRed.Click txtFirst.ForeColor = Color.Red Private Sub txtFirst_Leave(...) Handles txtFirst.Leave txtFirst.ForeColor = Color.Black 11/30/2018 Fundamentals of Programming in VB.Net

17 The Declaration Statement of an Event Procedure
A declaration statement for an event procedure: Private Sub btnOne_Click(...) Handles btnOne.Click The name can be changed at will. Private Sub ButtonPushed(...) Handling more than one event: Handles btnOne.Click, btnTwo.Click btnOne_Click is the name of the event procedure, and btnOne.Click identifies the event that triggers the procedure Also, an event procedure can be triggered by more than one event. 11/30/2018 Fundamentals of Programming in VB.Net

18 Fundamentals of Programming in VB.Net
Numbers 11/30/2018 Fundamentals of Programming in VB.Net

19 Fundamentals of Programming in VB.Net
Numbers Integer and Float Point Number Arithmetic Operations Variables Built-In Functions: Math.Sqrt Int Math.Round 11/30/2018 Fundamentals of Programming in VB.Net

20 Arithmetic Operations
Numbers are called numeric literals Five arithmetic operations in VB.NET + addition - subtraction * multiplication * 2 / division / 2 ^ exponentiation ^ 2 11/30/2018 Fundamentals of Programming in VB.Net

21 Parentheses of Arithmetic operation
Order of Operation: () Inner to outer,left to right ^ Left to right in expression * / Left to right in expression + - Left to right in expression 11/30/2018 Fundamentals of Programming in VB.Net

22 Fundamentals of Programming in VB.Net
Scientific Notation Scientific notation represents a number by using power of 10 to stand for zeros In VB.Net b • 10 r is written as bEr 11/30/2018 Fundamentals of Programming in VB.Net

23 Fundamentals of Programming in VB.Net
Scientific Notation r is a two digit number preceded by plus sign if r is positive and by a minus sign if r is negative 10 • 10 – ^ E-20 1.2 • * 10 ^ E+03 11/30/2018 Fundamentals of Programming in VB.Net

24 Fundamentals of Programming in VB.Net
Variables A variable is a name that is used to refer to an item of data. The value assigned to the variable may change. Up to 16,838 characters long Begin with a letter or an underscore Consist of only letters,digits and underscores A Variable name is written in lowercase letters except for the first letters of additional words, such as costOfIT201. 11/30/2018 Fundamentals of Programming in VB.Net

25 Fundamentals of Programming in VB.Net
Variables Declaration: Dim speed As Double Data type Variable name Assignment: speed = 50 11/30/2018 Fundamentals of Programming in VB.Net

26 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.) 11/30/2018 Fundamentals of Programming in VB.Net

27 Increment variable value
To add 1 to the numeric variable var var = var + 1 Or as a shortcut var +=1 11/30/2018 Fundamentals of Programming in VB.Net

28 Fundamentals of Programming in VB.Net
Built-in Functions Functions associates with one or more values and returns a value(output) Math.sqrt(n): Returns the square root of n. Math.Sqrt(9) is 3 Int(n): Returns greatest integer less than or equal to n Int(9.7) is 3 11/30/2018 Fundamentals of Programming in VB.Net

29 Fundamentals of Programming in VB.Net
Built-in Functions Math.Round(n, r): number n rounded to r decimal places Math.Round(2.317,1) is 2.3 Math.Round(2.7) is 3 When n is halfway between two successive whole number, then it rounds to the nearest even number Math.Round(2.5) is 2 Math.Round(3.5) is 4 11/30/2018 Fundamentals of Programming in VB.Net

30 Fundamentals of Programming in VB.Net
Integer Data Type An integer is a whole number Declaring an integer variable: Dim varName As Integer 11/30/2018 Fundamentals of Programming in VB.Net

31 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 11/30/2018 Fundamentals of Programming in VB.Net

32 Fundamentals of Programming in VB.Net
Three Types of Errors Syntax error – grammatical errors misspellings, omissions,incorrect punctuation Run-time error occurs when program is running Logic error program doesn’t perform as it is intended 11/30/2018 Fundamentals of Programming in VB.Net

33 Fundamentals of Programming in VB.Net
Strings 11/30/2018 Fundamentals of Programming in VB.Net

34 Fundamentals of Programming in VB.Net
Strings 11/30/2018 Fundamentals of Programming in VB.Net

35 Fundamentals of Programming in VB.Net
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 11/30/2018 Fundamentals of Programming in VB.Net

36 Fundamentals of Programming in VB.Net
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 11/30/2018 Fundamentals of Programming in VB.Net

37 Using Text Boxes for Input and Output
The contents of a text box is a string input strVar = txtBox.Text Output (ReadOnly = true) txtBox.Text = strVar 11/30/2018 Fundamentals of Programming in VB.Net

38 Fundamentals of Programming in VB.Net
Data Conversion Type-casting Converts data from one type to another such as CDblr, Cint, CStr 11/30/2018 Fundamentals of Programming in VB.Net

39 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) Convert.ToString(numVar) numVar.ToString Str(numVar) 11/30/2018 Fundamentals of Programming in VB.Net

40 Fundamentals of Programming in VB.Net
Data Conversion All conversions to String are considered to be widening, regardless of whether strict semantics are used. Special case Nothing - empty string literal "" 11/30/2018 Fundamentals of Programming in VB.Net

41 Fundamentals of Programming in VB.Net
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 11/30/2018 Fundamentals of Programming in VB.Net

42 Fundamentals of Programming in VB.Net
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 11/30/2018 Fundamentals of Programming in VB.Net

43 Fundamentals of Programming in VB.Net
ANSI Character Set A numeric representation for every key on the keyboard. The numbers are ranging from 32 to 255 11/30/2018 Fundamentals of Programming in VB.Net

44 Fundamentals of Programming in VB.Net
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”) 32 & Chr(176) & “Fahrenheit ° Fahrenheit 11/30/2018 Fundamentals of Programming in VB.Net

45 String Properties and Methods
A string is an object, like controls, has both properties and methods. Length ToUpper Trim ToLower IndexOf Substring 11/30/2018 Fundamentals of Programming in VB.Net

46 Fundamentals of Programming in VB.Net
String Properties str.Length: the number of characters in str “Tacoma".Length is 5 11/30/2018 Fundamentals of Programming in VB.Net

47 Fundamentals of Programming in VB.Net
String methods str.ToUpper with all letters of str capitalized “Tacoma".ToUpper is TACOMA. str.ToLower with all letters of str in lowercase format “TCC1926".ToLower is tcc1926 11/30/2018 Fundamentals of Programming in VB.Net

48 Fundamentals of Programming in VB.Net
String Methods str.Trim with all leading and trailing spaces deleted “IT” & “ 201 ".Trim is IT201 11/30/2018 Fundamentals of Programming in VB.Net

49 Fundamentals of Programming in VB.Net
String Methods str.Substring(m,n) 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” 11/30/2018 Fundamentals of Programming in VB.Net

50 Fundamentals of Programming in VB.Net
String Methods str.IndexOf(substr) -1 if substr is a substring of str beginning position of the first occurrence of substr in str "fanatic".IndexOf("ati") is 3. "fanatic".IndexOf("nt") is –1. 11/30/2018 Fundamentals of Programming in VB.Net

51 Fundamentals of Programming in VB.Net
String Methods str.IndexOf(substr,n) the position of the first occurrence of substr in str in position n or greater "fanatic".IndexOf(“a”, 3) is 3. 11/30/2018 Fundamentals of Programming in VB.Net

52 Fundamentals of Programming in VB.Net
The Empty String zero-length string "" lstBox.Items.Add("") skips a line txtBox.Text = "“ or txtBox.Clear() clear the text box 11/30/2018 Fundamentals of Programming in VB.Net

53 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“ 11/30/2018 Fundamentals of Programming in VB.Net

54 Fundamentals of Programming in VB.Net
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 11/30/2018 Fundamentals of Programming in VB.Net

55 Fundamentals of Programming in VB.Net
Option Strict The default is Off Option Strict { On | Off } On: Optional. Enables Option Strict checking Off : Optional. Disables Option Strict checking 11/30/2018 Fundamentals of Programming in VB.Net

56 Fundamentals of Programming in VB.Net
Option Strict Example Option Strict On Dim MyVar As Integer Dim Obj As Object MyVar = 1000 MyVar = ‘ Data lost MyInt = 10 ' Undeclared variable 11/30/2018 Fundamentals of Programming in VB.Net

57 Internal Documentation
Begin the line with an apostrophe Benefits: Easy to understand the program Easy to read long programs because the purposes of individual pieces can be determined at a glance. 11/30/2018 Fundamentals of Programming in VB.Net

58 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)" 11/30/2018 Fundamentals of Programming in VB.Net

59 Fundamentals of Programming in VB.Net
Input and Output 11/30/2018 Fundamentals of Programming in VB.Net

60 Fundamentals of Programming in VB.Net
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 11/30/2018 Fundamentals of Programming in VB.Net

61 Fundamentals of Programming in VB.Net
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 11/30/2018 Fundamentals of Programming in VB.Net

62 Formatting Output with Format Functions
String Value FormatNumber( ,1) FormatNumber(1 + 2) 12,345.6 3.00 FormatCurrency( ,2) FormatCurrency(-100) $12,345.63 ($100.00) FormatPercent(0.185,2) FormatPercent(“0.07”) 18.50% 7.00% 11/30/2018 Fundamentals of Programming in VB.Net

63 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. 11/30/2018 Fundamentals of Programming in VB.Net

64 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 11/30/2018 Fundamentals of Programming in VB.Net

65 Formatting output with zones
A colon and formatting symbol after width to specially format numeric data Zone Format Number to be formatted Number displayed {1,12:N3} {1,12:N0} 34.6 34 {1,12:C1} $1234.6 {1,-12:P} 0.569 56.90% 11/30/2018 Fundamentals of Programming in VB.Net

66 Formatting Output 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. 11/30/2018 Fundamentals of Programming in VB.Net

67 Formatting Output with Zones
Dim fmtStr As String = “{0,10} {1,12}” With 1stOutput.Items .Add(“123”) End With Displays 11/30/2018 Fundamentals of Programming in VB.Net

68 Fundamentals of Programming in VB.Net
Inputting Data Read data stored in files and accessed with a StreamReader object Supplied by the user with an input dialog box. InputBox(prompt, title) 11/30/2018 Fundamentals of Programming in VB.Net

69 Fundamentals of Programming in VB.Net
Input data from a file Establish a communication link between computer and the disk drive for reading data from disk Dim readerVar As IO.StreamReader = _ IO.File.OpenText(filespec) Read data in order, one at a time, from the file with the ReadLine method assuming the file contains one item of data per line. strVar = readerVar.ReadLine 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() 11/30/2018 Fundamentals of Programming in VB.Net

70 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") 11/30/2018 Fundamentals of Programming in VB.Net

71 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") 11/30/2018 Fundamentals of Programming in VB.Net


Download ppt "Fundamentals of Programming in VB.NET"

Similar presentations


Ads by Google