1 / 63

Chapter 3: Using Variables and Constants

Chapter 3: Using Variables and Constants. Programming with Microsoft Visual Basic .NET, Second Edition. Exercise #1. lblDisplay. txtDisplay. btnClear. btnDisplay. btnExit. Solution Exercise #1.

pules
Download Presentation

Chapter 3: Using Variables and Constants

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. Chapter 3: Using Variables and Constants Programming with Microsoft Visual Basic .NET, Second Edition

  2. Exercise #1 lblDisplay txtDisplay btnClear btnDisplay btnExit

  3. Solution Exercise #1 Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click lblDisplay.text = “Hello” End Sub Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click lblDisplay.text = “” End Sub Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click Me.Close() End Sub End Class

  4. Solution Exercise #2 • Design the UI • Find the object’s “property” that allows to hide a button • Find the right event that must be associated with the buttons • Write the code associated with each event On click - Exit

  5. Solution Exercise #2 (properties) Private Sub btnNoLeft_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNoLeft.MouseHover btnNoRight.Visible = True btnNoLeft.Visible = False End Sub Private Sub btnNoRight_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNoRight.MouseHover btnNoLeft.Visible = True btnNoRight.Visible = False End Sub Private Sub btnYes_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnYes.Click Me.Close() End Sub End Class

  6. Solution Exercise #2 (methods) Private Sub btnNoLeft_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNoLeft.MouseHover btnNoRight.Show() btnNoLeft.Hide() End Sub Private Sub btnNoRight_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNoRight.MouseHover btnNoLeft.Show() btnNoRight.Hide() End Sub Private Sub btnYes_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnYes.Click Me.Close() End Sub End Class

  7. Creating Variables and Named Constants Lesson A Objectives • Create a procedure-level and module-level variable • Select an appropriate data type for a variable • Select an appropriate name for a variable Programming with Microsoft Visual Basic .NET, Second Edition

  8. Creating Variables and Named Constants Lesson A Objectives (continued) • Assign data to an existing variable • Explain the scope and lifetime of a variable • Create a named constant Programming with Microsoft Visual Basic .NET, Second Edition

  9. Problem A txtLeft txtRight Hello Move  Programming with Microsoft Visual Basic .NET, Second Edition

  10. Problem B txtLeft txtRight Bonjour Hello Permute  Programming with Microsoft Visual Basic .NET, Second Edition

  11. Container problem! Programming with Microsoft Visual Basic .NET, Second Edition

  12. Container solution! 1 3 2 Programming with Microsoft Visual Basic .NET, Second Edition

  13. Variables • In addition to receiving user input with text boxes and displaying information with labels (or text boxes) a program generally needs to perform computations on the input to produce the output. • To store the result of the computations while the program is running, you use variables. • Avariableis a programming element used to store a value in the program. A variable is much like a box where you can store and retrieve information Programming with Microsoft Visual Basic .NET, Second Edition

  14. Using Variables to Store Information • Besides storing data in the properties of controls, a programmer also can store data, temporarily, in memory locations inside the computer • The memory locations are called variables, because the contents of the locations can change as the program is running Programming with Microsoft Visual Basic .NET, Second Edition

  15. Using Variables to Store Information (continued) • One use for a variable is to hold information that is not stored in a control on the user interface • You can also store the data contained in a control’s property in a variable Programming with Microsoft Visual Basic .NET, Second Edition

  16. Variables • Like an object, every variable you create as aname. • Every variable also has a data type that determines the kind of output the variable can store. • A variable can store eithertextual information or numbers. Programming with Microsoft Visual Basic .NET, Second Edition

  17. Selecting a Data Type for a Variable Programming with Microsoft Visual Basic .NET, Second Edition

  18. Data type = Memory size Programming with Microsoft Visual Basic .NET, Second Edition

  19. Selecting a Data Type for a Variable (continued) Programming with Microsoft Visual Basic .NET, Second Edition

  20. 10. String Group of characters Depends Programming with Microsoft Visual Basic .NET, Second Edition

  21. Selecting a Name for a Variable • The naming convention used in this book: • The name indicates only the variable’s purpose and is entered using lowercase letters • Use camel casing: if a variable’s name contains two or more words, you capitalize the first letter in the second and subsequent words • The name assigned to a variable must follow the rules listed in Figure 3-5 Programming with Microsoft Visual Basic .NET, Second Edition

  22. Programming with Microsoft Visual Basic .NET, Second Edition

  23. Programming with Microsoft Visual Basic .NET, Second Edition

  24. Declaring a Variable • You use a declaration statement to declare, or create, a variable • Syntax: {Dim | Private | Static} variablename [As datatype][= initialvalue] • Examples: • Dim intHoursWorked As Integer • Dim blnDataOk As Boolean = True • Dim strName As String, intAge As Integer Programming with Microsoft Visual Basic .NET, Second Edition

  25. Assigning Data to an Existing Variable • You use an assignment statement to assign a value to a variable while an application is running • Syntax: variablename = value • Examples: • shtQuantityOrdered = 500 • strFirstName = “Mary” • strState = txtState.Text • sngDiscountRate = .03 Programming with Microsoft Visual Basic .NET, Second Edition

  26. Problem B txtLeft txtRight Bonjour Hello Permute  Programming with Microsoft Visual Basic .NET, Second Edition

  27. Problem B Dim strTemp as String strTemp = txtLeft.text txtLeft.text = txtRight.text txtRight.text = strTemp Programming with Microsoft Visual Basic .NET, Second Edition

  28. Assigning Data to an Existing Variable (continued) • A literal constant is an item whose value does not change while the application is running • String literal constants are enclosed in quotation marks, but numeric literal constants and variable names are not • A literal type character forces a literal constant to assume a data type other than the one its form indicates Programming with Microsoft Visual Basic .NET, Second Edition

  29. Assigning Data to an Existing Variable (continued) Figure 3-7: Literal type characters Programming with Microsoft Visual Basic .NET, Second Edition

  30. Assigning Data to an Existing Variable (continued) • A variable can store only one item of data at any one time • When you use an assignment statement to assign another item to the variable, the new data replaces the existing data • After data is stored in a variable, you can use the data in calculations Programming with Microsoft Visual Basic .NET, Second Edition

  31. Textboxes contain Strings 1 Price 1: Dim strPrice1 as string Dim strPrice2 as string Dim sngTotal as single strPrice1 = txtPrice1.text strPrice2 = txtPrice2.text sngTotal= strPrice1 + strPrice2 2 Price 2: sngTotal = ? Programming with Microsoft Visual Basic .NET, Second Edition

  32. The Parse Method • Every numeric data type in Visual Basic .NET has a Parse method that can be used to convert a string to that numeric data type • Syntax: numericDataType.Parse(string) • Example:Dim decSales As Decimal decSales = Decimal.Parse(txtSales.Text) Programming with Microsoft Visual Basic .NET, Second Edition

  33. The Convert Class • The Convert class contains methods to convert a numeric value to a specified data type • Syntax: Convert.method(value) • Example:Dim dblPurchase As Double = 500 Dim decTax As Decimal decTax = Convert.ToDecimal(dblPurchase) * .03 Programming with Microsoft Visual Basic .NET, Second Edition

  34. The Convert Class (continued) Example: Convert.ToDecimal(VariableName) Figure 3-9: Most commonly used methods contained in the Convert class Programming with Microsoft Visual Basic .NET, Second Edition

  35. The Scope and Lifetime of a Variable • A variable’s scope indicates which procedures in an application can use the variable • The scope is determined by where the Dim, Public or Private statement is entered • When you declare a variable in a procedure, the variable is called a procedure-level variable and is said to have procedure scope Programming with Microsoft Visual Basic .NET, Second Edition

  36. The Scope and Lifetime of a Variable (continued) • When you declare a variable in the form’s Declarations section, it is called a module-level variable and is said to have module scope • Block-level variables are declared within specific blocks of code, such as within If...Then...Else statements or For...Next statements Programming with Microsoft Visual Basic .NET, Second Edition

  37. The Scope and Lifetime of a Variable (continued) • Creating a procedure-level variable • Created with the Dim keyword • The Dim statement is entered in an object’s event procedure • Only the procedure in which it is declared can use the variable • Removed from memory when the procedure ends Programming with Microsoft Visual Basic .NET, Second Edition

  38. Named Constants • A memory location whose contents cannot be changed while the program is running • You create a named constant using the Const statement • Syntax: Const constantname [As datatype] = expression • Example: • Const dblPI As Double = 3.141593 Programming with Microsoft Visual Basic .NET, Second Edition

  39. Option Explicit and Option Strict • Visual Basic .NET provides a way to prevent you from using undeclared variables in your code • Enter the statement Option Explicit On in the General Declarations section of the Code Editor window Programming with Microsoft Visual Basic .NET, Second Edition

  40. Implicit type conversion Dim intNumber1 as Integer =1 Dim sngNumber2 as single =2.5 sngNumber2 = intNumber1 Dim intNumber1 as Integer =1 Dim sngNumber2 as single =2.5 intNumber1 = sngNumber2 Loss of precision! Programming with Microsoft Visual Basic .NET, Second Edition

  41. Option Explicit and Option Strict (continued) • To eliminate the problems that occur as a result of implicit type conversions • Enter the Option Strict On statement in the General Declarations section of the Code Editor window Programming with Microsoft Visual Basic .NET, Second Edition

  42. Option Explicit and Option Strict (continued) • Type conversion rules used when the Option Strict On statement is used: • Strings will not be implicitly converted to numbers, and numbers will not be implicitly converted to strings • Lower-ranking data types will be implicitly promoted to higher-ranking types • Higher-ranking data types will not be implicitly demoted to lower-ranking data types; rather, a syntax error will occur Programming with Microsoft Visual Basic .NET, Second Edition

  43. Concatenating Strings • Connecting strings together is called concatenating • Use the concatenation operator, which is the ampersand (&), to concatenate strings in Visual Basic .NET • When concatenating strings, be sure to include a space before and after the concatenation operator Programming with Microsoft Visual Basic .NET, Second Edition

  44. Concatenating Strings (continued) Programming with Microsoft Visual Basic .NET, Second Edition

  45. The InputBox Function • The InputBox function displays one of Visual Basic .NET’s predefined dialog boxes • Syntax: InputBox(prompt[, title][, defaultResponse]) • Use sentence capitalization for the prompt, and book title capitalization for the title • Has limitations: can’t control appearance and allows user to enter only one piece of data Programming with Microsoft Visual Basic .NET, Second Edition

  46. Input Box StrUserInput = InputBox (Prompt,Title, Default) strAge = InputBox(“Enter your age” , ”InputBox Age”) Optional Programming with Microsoft Visual Basic .NET, Second Edition

  47. The InputBox Function (continued) Data collected through an input box is always considered as a String! InputBox(prompt[, title][, defaultResponse]) sngHours = Single.Parse(InputBox("Enter the number of hours worked", “Hours Entry“,”40”)) Figure 3-29: Example of a dialog box created by the InputBox function Programming with Microsoft Visual Basic .NET, Second Edition

  48. The NewLine Character • The NewLine character, which is Chr(13) & Chr(10), instructs the computer to issue a carriage return followed by a line feed • The ControlChars.NewLine constant advances the insertion point to the next line on the screen • The ControlChars.NewLine constant is an intrinsic constant, which is a named constant that is built into Visual Basic .NET Programming with Microsoft Visual Basic .NET, Second Edition

  49. The NewLine Character lblDisplay.text = “Hello” & Chr(13) & Chr(10) & “Vincent” lblDisplay.text = “Hello” & ControlChars.NewLine & “Vincent” Hello Vincent Programming with Microsoft Visual Basic .NET, Second Edition

More Related