1 / 93

Review of Chapter 2

Review of Chapter 2. How to Develop a VB Application. Design the Interface for the user Literally draw the GUI Drag buttons/text boxes/etc onto form Determine which events the controls on the window should recognize Write the code for those events. What happens when program is running.

urian
Download Presentation

Review of Chapter 2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Review of Chapter 2

  2. How to Develop a VB Application • Design the Interface for the user • Literally draw the GUI • Drag buttons/text boxes/etc onto form • Determine which events the controls on the window should recognize • Write the code for those events

  3. What happens when program is running • VB monitors the controls for events • If event occurs, it runs procedures assigned to that event • If no event exists, it goes back to #1.

  4. Initial Visual Basic Screen

  5. Properties Window Selected control Properties Settings

  6. Control Name Prefixes

  7. Positioning Controls Proximity line

  8. Aligning Controls Snap line

  9. Code Editor Code Editor tab Form Designer tab Method Name box Class Name box

  10. Sample Code Public ClassfrmDemo Private SubtxtFirst_TextChanged(...) Handles txtFirst.TextChanged txtFirst.ForeColor = Color.Blue End Sub End Class

  11. Chapter 3

  12. Variables, Input, and Output • 3.1 Numbers • 3.2 Strings • 3.3 Input and Output

  13. Arithmetic Operations • Numbers are called numeric literals • Five arithmetic operations in Visual Basic • + addition • - subtraction • * multiplication • / division • ^ exponentiation

  14. Numeric Expressions • 2 + 3 • 3 * (4 + 5) • 2 ^ 3

  15. Displaying Numbers Let n be a number or a numeric expression. What does the statement lstBox.Items.Add(n) do?

  16. Example 1: Form

  17. Example 1: Code and Output Private SubbtnCompute_Click(...) HandlesbtnCompute.Click lstResults.Items.Add(5) lstResults.Items.Add(2 * 3) lstResults.Items.Add((2 ^ 3) – 1) End Sub What is the result?

  18. Numeric Variable A numeric variable is a name to which a number can be assigned. Examples: speed distance interestRate balance

  19. Variables • Declaration: Dim speed As Double Data type Variable name • Assignment: • speed = 50

  20. Variables

  21. Variables

  22. Initialization • Numeric variables are automatically initialized to 0: DimvarName As Double • To specify a nonzero initial value DimvarName As Double = 50

  23. Numeric Expressions Numeric variables can be used in numeric expressions Dim balanceAs Double = 1000 lstBox.Items.Add(1.05 * balance)

  24. Assignment Statement Dim numVar1As Double = 5 Dim numVar2 As Double = 4 numVar1 = 3 * numVar2 lstBox.Items.Add(numVar1)

  25. Incrementing • To add 1 to the numeric variable var var = var + 1 • Or as a shortcut var += 1 • Or as a generalization var += numeric expression

  26. Built-in Functions • Functions return a value Math.Sqrt(9) returns 3 Int(9.7) returns 9 Math.Round(2.7) is 3

  27. Integer Data Type • Variables of type Double can be assigned both whole numbers and numbers with decimals • The statement DimvarName As Integer declares a numeric variable that can only be assigned whole number values between about -2 billion and 2 billion

  28. 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

  29. Parentheses • Parentheses should be used liberally in numeric expressions • In the absence of parentheses, the operations are carried out in the following order: ^, * and /, + and -

  30. Three Types of Errors • Syntax error • Run-time error • Logic error

  31. Some Types of Syntax Errors • Misspellings lstBox.Itms.Add(3) • Omissions lstBox.Items.Add(2 + ) • Incorrect punctuation Dim m; n As Integer Displayed as blue underline in VS

  32. A Type of Run-time Error Dim numVar As Integer = 1000000 numVar = numVar * numVar What’s wrong with the above?

  33. A Logical Error Dim average As Double Dim m As Double = 5 Dim n As Double = 10 average = m + n / 2 What’s wrong with the above?

  34. Error List Window • Dim m; n As Double • lstResults.Items.Add(5 • lstResults.Items.Add(a)

  35. – Variables, Input, and Output • 3.1 Numbers • 3.2 Strings • 3.3 Input and Output

  36. String Literal A string literal is a sequence of characters surrounded by quotation marks. Examples: "hello" "123-45-6789" "#ab cde?"

  37. String Literal A string literal is a sequence of characters surrounded by quotation marks. Examples: Does this work? “She said: “I’m tired.””

  38. String Variable A string variable is a name to which a string value can be assigned. Examples: country ssn word firstName

  39. String Variable • Declaration: Dim firstName As String Data type Variable name • Assignment: • firstName = "Fred"

  40. String Variable You can declare a string variable and assign it a value at the same time. DimfirstNameAs String = "Fred"

  41. Add Method Let str be a string literal or variable. Then, lstBox.Items.Add(str) displays the value of str in the list box.

  42. String Variable You can assign the value of one string variable to another Dim strVar1 As String = "Hello" Dim strVar2 As String = "Goodbye" strVar2 = strVar1 lstOutput.Items.Add(strVar2)

  43. Variables and Strings Private Sub btnDisplay_Click(...) Handles btnDisplay.Click Dim president As String president = "George Washington" lstOutput.Items.Add("president") lstOutput.Items.Add(president) End Sub

  44. Option Strict • Visual Basic allows numeric variables to be assigned strings and vice versa, a poor programming practice. • To prevent such assignments, set Option Strict to On in the Options dialog box.

  45. Option Strict -continued • Select Options from the Tools menu • In left pane, expand Projects and Solution • Select VB Defaults • Set Option Strict to On

  46. Text Boxes for Input & Output • The contents of a text box is always a string • Input example strVar = txtBox.Text • Output example txtBox.Text = strVar

  47. Data Conversion • Because the contents of a text box is always a string, sometimes you must convert the input or output dblVar = CDbl(txtBox.Text) txtBox.Text = CStr(numVar) Converts a String to a Double Converts a number to a string

  48. Widening and Narrowing • Widening: assigning an Integer value to a Double variable • Widening always works. (Every Integer is a Double.) • No conversion function needed. • Narrowing: assigning a Double value to an Integer variable • Narrowing might not work. (Not every Double is an Integer.) • Narrowing requires Cint. • Will loose information (everything after the decimal place) • Strings can be given a different initial value as follows

  49. Auto Correction

  50. With Option Strict On Dim dblVar As Double, intVar As Integer Dim strVar As String Not Valid: Replace with: intVar = dblVar intVar = CInt(dblVar) dblVar = strVar dblVar = CDbl(strVar) strVar = intVar strVar = CStr(intVar)

More Related