Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming with Microsoft Visual Basic 2010 5 th Edition CHAPTER THREE USING VARIABLES AND CONSTANTS.

Similar presentations


Presentation on theme: "Programming with Microsoft Visual Basic 2010 5 th Edition CHAPTER THREE USING VARIABLES AND CONSTANTS."— Presentation transcript:

1 Programming with Microsoft Visual Basic 2010 5 th Edition CHAPTER THREE USING VARIABLES AND CONSTANTS

2 Programming with Microsoft Visual Basic 2010, 5 th Edition Previewing the Modified Playtime Cellular Application Previewing the Playtime Cellular application ◦Access Run command on Start menu ◦Browse to VB2010\Chap03 folder ◦Click the Playtime Cellular (Playtime Cellular.exe) file ◦View order form Enter customer information from pages 119-120 Completed application resembles Chapter 2 version 2

3 Programming with Microsoft Visual Basic 2010, 5 th Edition 3 Figure 3-2 Completed order form

4 Programming with Microsoft Visual Basic 2010, 5 th Edition Lesson A Objectives After studying Lesson A, you should be able to: Declare variables and named constants Assign data to an existing variable Convert string data to a numeric data type using the TryParse method Convert numeric data to a different data type using the Convert class methods 4

5 Programming with Microsoft Visual Basic 2010, 5 th Edition Lesson A Objectives (cont’d.) Explain the scope and lifetime of variables and named constants Explain the purpose of the Option Explicit, Option Infer, and Option Strict 5

6 Programming with Microsoft Visual Basic 2010, 5 th Edition Using Variables to Store Information Controls and variables temporarily store data Variable: Temporary storage location in main memory ◦Specified by data type, name, scope, and lifetime Reasons to use variables ◦Hold information that is not stored in control on form ◦Allow for more precise treatment of numeric data ◦Enable code to run more efficiently 6

7 Programming with Microsoft Visual Basic 2010, 5 th Edition Using Variables to Store Information (cont’d.) Selecting a data type for a variable ◦Data type: Specifies type of data a variable can store ◦Provides a class template for creating variables Unicode ◦Universal coding scheme for characters ◦Assigns unique numeric value to each character in the written languages of the world 7

8 Programming with Microsoft Visual Basic 2010, 5 th Edition 8 Figure 3-3 Basic data types in Visual Basic

9 Programming with Microsoft Visual Basic 2010, 5 th Edition Using Variables to Store Information (cont’d.) For this course: ◦Use Integer data type for all integers ◦Use either Decimal or Double data type for numbers containing decimal places or numbers used in calculations ◦Use String data type for text or numbers not used in calculations ◦Use Boolean data type for Boolean values 9

10 Programming with Microsoft Visual Basic 2010, 5 th Edition Using Variables to Store Information (cont’d.) Selecting a name for a variable ◦Variables are referred to by name ◦Identifier: Another term for variable name Guidelines for naming variables ◦Use Hungarian notation, with a three-character prefix representing the variable’s data type ◦Name should be descriptive: e.g., dblLength ◦Use camel case: e.g., dblSalesAmount Must follow variable naming rules 10

11 Programming with Microsoft Visual Basic 2010, 5 th Edition 11 Figure 3-4 Variable naming rules and examples

12 Programming with Microsoft Visual Basic 2010, 5 th Edition Using Variables to Store Information (cont’d.) Declaring a variable ◦Declaration statement: Used to declare (create) a variable and reserve space in memory for it Syntax shown in Figure 3-5 on next slide If no initial value is given to variable when declaring it, computer stores default value ◦Numeric variables are set to 0 ◦Boolean variables are set to False ◦Object and String variables are set to Nothing ◦Date variables are set to 1/1/0001 12:00:00AM 12

13 Programming with Microsoft Visual Basic 2010, 5 th Edition 13 Figure 3-5 Syntax and examples of a variable declaration statement

14 Programming with Microsoft Visual Basic 2010, 5 th Edition Assigning Data to an Existing Variable Assignment statement: Assigns value to variable at run time ◦Syntax: variablename = expression ◦Expression may include literal constants, object properties, variables, keywords, arithmetic operators Literal constant ◦Data item whose value does not change ◦Example: The string “Mary” Literal type character ◦Forces literal constant to change data type 14

15 Programming with Microsoft Visual Basic 2010, 5 th Edition Assigning Data to an Existing Variable (cont’d.) TryParse method: Converts string to number TryParse is preferred over Val ◦Allows programmer to specify data type ◦Val only returns a type Double value Syntax shown in Figure 3-7 on next slide ◦ dataType : Numeric data type, such as Integer ◦ string : String to be converted ◦ variable : Variable that receives the numeric value 15

16 Programming with Microsoft Visual Basic 2010, 5 th Edition 16 Figure 3-7 Basic syntax and examples of the TryParse method

17 Programming with Microsoft Visual Basic 2010, 5 th Edition Assigning Data to an Existing Variable (cont’d.) Convert class: Can be used to convert a number from one type to another Syntax shown in Figure 3-9 on next slide – Convert : Name of class – method : Converts value to specified data type – value : Numeric data to be converted TryParse is recommended for converting strings to numeric data types ◦Will not produce an error if conversion fails 17

18 Programming with Microsoft Visual Basic 2010, 5 th Edition 18 Figure 3-9 Syntax and examples of the Convert class methods

19 Programming with Microsoft Visual Basic 2010, 5 th Edition The Scope and Lifetime of a Variable Scope: Indicates where variable can be used Lifetime: How long variable remains in memory Scope and lifetime are determined by where variable is declared Three types of scope ◦Class: Variable can be used by all procedures in a form ◦Procedure: Variable can be used within procedure ◦Block: Variable can be used within specific code block 19

20 Programming with Microsoft Visual Basic 2010, 5 th Edition The Scope and Lifetime of a Variable (cont’d.) Variables with procedure scope ◦Can be used only by that procedure ◦Declared at beginning of procedure ◦Removed from memory when procedure ends ◦Declared using Dim keyword Most variables used in this course will be procedure-level variables Sales tax example UI and code given on following slides illustrate use of procedure variables 20

21 Programming with Microsoft Visual Basic 2010, 5 th Edition The Scope and Lifetime of a Variable (cont’d.) 21 Figure 3-10 User interface for the Sales Tax Calculator application

22 Programming with Microsoft Visual Basic 2010, 5 th Edition 22 Figure 3-11 Click event procedures using procedure-level variables

23 Programming with Microsoft Visual Basic 2010, 5 th Edition The Scope and Lifetime of a Variable (cont’d.) Variables with class scope ◦Can be used by all procedures in a form ◦Declared in form’s Declarations section ◦Remain in memory until application ends ◦Declared using Private keyword Total Sales example UI and code given on following slides illustrate use of class-level variables 23

24 Programming with Microsoft Visual Basic 2010, 5 th Edition The Scope and Lifetime of a Variable (cont’d.) 24 Figure 3-12 User interface for the Total Sales application

25 Programming with Microsoft Visual Basic 2010, 5 th Edition 25 Figure 3-13 Code using a class-level variable

26 Programming with Microsoft Visual Basic 2010, 5 th Edition Static Variables Static variable: Procedure-level variable with extended lifetime ◦Remains in memory between procedure calls ◦Retains its value even when the procedure ends Static keyword: Used to declare static variable Static variables act like class-level variables but have narrower scope ◦Can only be used within procedure where declared 26

27 Programming with Microsoft Visual Basic 2010, 5 th Edition 27 Figure 3-14 Code using a static variable

28 Programming with Microsoft Visual Basic 2010, 5 th Edition Named Constants Named constant ◦Memory location inside computer whose contents cannot be changed at run time Const statement: Creates named constant ◦Stores value of expression in named constant ◦expression: Can be literal constant, another named constant, or an arithmetic operator ◦Cannot contain a variable or method Syntax and examples shown in Figure 3-15 on next slide 28

29 Programming with Microsoft Visual Basic 2010, 5 th Edition 29 Figure 3-15 Syntax and examples of the Const statement

30 Programming with Microsoft Visual Basic 2010, 5 th Edition 30 Figure 3-17 Calculate Area button’s Click event procedure Figure 3-16 User interface for the Area Calculator application

31 Programming with Microsoft Visual Basic 2010, 5 th Edition Option Explicit, Option Infer, and Option Strict Option Explicit On statement ◦Prevents you from using undeclared variables Implicit type conversion: Converts right-side value to the data type of left side ◦Promotion ◦Data converted to greater precision number ◦e.g., Integer to Decimal ◦Demotion ◦Data truncated ◦e.g., Decimal to Integer ◦Data loss can occur when demotion occurs 31

32 Programming with Microsoft Visual Basic 2010, 5 th Edition Option Explicit, Option Infer, and Option Strict (cont’d.) Option Infer Off statement: ◦Ensures that every variable is declared with a data type Option Strict On statement: ◦Disallows implicit conversions ◦Type conversion rules are applied when this option is on ◦Figure 3-18 on following slide contains examples 32

33 Programming with Microsoft Visual Basic 2010, 5 th Edition 33 Figure 3-18 Rules and examples of type conversions

34 Programming with Microsoft Visual Basic 2010, 5 th Edition Option Explicit, Option Infer, and Option Strict (cont’d.) 34 Figure 3-19 Option statements entered in the General Declarations section

35 Programming with Microsoft Visual Basic 2010, 5 th Edition Lesson A Summary Declare a variable using {Dim | Private | Static} Assignment statement: Assigns value to a variable Three levels of scope: Block, procedure, class TryParse () converts strings to numeric data Use Const to declare a named constant Avoid programming errors by using Option Explicit On, Option Infer Off, and Option Strict On 35

36 Programming with Microsoft Visual Basic 2010, 5 th Edition Lesson B Objectives After studying Lesson B, you should be able to: Include procedure-level and class-level variables in an application Concatenate strings Get user input using the InputBox function Include the ControlChars.NewLine constant in code Designate the default button for a form Format numbers using the ToString method 36

37 Programming with Microsoft Visual Basic 2010, 5 th Edition Modifying the Playtime Cellular Application Modifications needed: ◦Calculate and display the sales tax ◦Display salesperson name Revise the TOE chart to reflect the new tasks Must modify btnCalc button’s Click event and the form’s Load event 37

38 Programming with Microsoft Visual Basic 2010, 5 th Edition Modifying Playtime Application 38

39 Programming with Microsoft Visual Basic 2010, 5 th Edition Modifying Playtime Application 39

40 Programming with Microsoft Visual Basic 2010, 5 th Edition 40 Figure 3-20 Revised TOE chart for the Playtime Cellular application

41 Programming with Microsoft Visual Basic 2010, 5 th Edition Modifying the Calculate Order Button’s Code General strategy ◦Remove existing code from Click event procedure ◦Recode the procedure using variables in equations Use Option Explicit On statement ◦Enforces full variable declaration Use Option Infer Off statement ◦Enforces that variables are declared with data types Use Option Strict On statement ◦Suppresses implicit type conversions 41

42 Programming with Microsoft Visual Basic 2010, 5 th Edition 42 Figure 3-22 Jagged blue lines indicate errors in the statements Figure 3-23 Lines to delete from the procedure

43 Programming with Microsoft Visual Basic 2010, 5 th Edition Modifying the Calculate Order Button’s Code (cont’d.) 43 Figure 3-24 Revised pseudocode for the btnCalc control’s Click event procedure

44 Programming with Microsoft Visual Basic 2010, 5 th Edition Modifying the Calculate Order Button’s Code (cont’d.) 44 Figure 3-25 List of named constants and variables

45 Programming with Microsoft Visual Basic 2010, 5 th Edition Modifying the Calculate Order Button’s Code (cont’d.) 45 Figure 3-26 Const and Dim statements entered in the procedure

46 Programming with Microsoft Visual Basic 2010, 5 th Edition 46 Figure 3-27 Code entered in the btnCalc control’s Click event procedure

47 Programming with Microsoft Visual Basic 2010, 5 th Edition Concatenating Strings Concatenate: Connect strings together Concatenation operator: Ampersand (&) ◦Include space before and after & operator Numeric values used with the & operator are converted to strings 47

48 Programming with Microsoft Visual Basic 2010, 5 th Edition Concatenating Strings (cont’d.) 48 Figure 3-29 Examples of string concatenation

49 Programming with Microsoft Visual Basic 2010, 5 th Edition The InputBox Function InputBox function ◦Displays dialog box and retrieves user input Arguments ◦ prompt : Message to display inside dialog box ◦ title : Text to display in the dialog box’s title bar ◦ defaultResponse : Text to be displayed in the input field Returned value most often assigned to String variable Syntax shown in Figure 3-33 on next slide 49

50 Programming with Microsoft Visual Basic 2010, 5 th Edition 50 Figure 3-33 Basic syntax and examples of the InputBox function

51 Programming with Microsoft Visual Basic 2010, 5 th Edition The ControlChars.Newline Constant ControlChars.NewLine constant ◦Advances the insertion point to the next line in a control ◦Also used to advance insertion point in file or on printer To use, type ControlChars.NewLine at appropriate location ◦Can be used with string concatenation Line continuation character (_) ◦Used to break up long line of code into two or more lines 51

52 Programming with Microsoft Visual Basic 2010, 5 th Edition The ControlChars.Newline Constant (cont’d.) 52 Figure 3-37 Modified assignment statement

53 Programming with Microsoft Visual Basic 2010, 5 th Edition Designating a Default Button Default button ◦Button that is activated by pressing Enter key ◦Button is not required to have the focus ◦Only one per form Default button should be button used most often by the user ◦Except if button’s task is destructive and irreversible, such as deleting data Set form’s AcceptButton property to button name 53

54 Programming with Microsoft Visual Basic 2010, 5 th Edition Using the ToString Method to Format Numbers Formatting: Specifying decimal places and special characters to display ToString method is replacing Format function Syntax: variablename.ToString(formatString) ◦ variablename : Name of a numeric variable ◦ formatString : String specifying format you want to use format String has form of Axx specifying a format and precision specifier 54

55 Programming with Microsoft Visual Basic 2010, 5 th Edition Printing Euro sign Thread.CurrentThread.CurrentCulture = New CultureInfo("fr-FR") 55

56 Programming with Microsoft Visual Basic 2010, 5 th Edition 56 Figure 3-40 Syntax and examples of the ToString method

57 Programming with Microsoft Visual Basic 2010, 5 th Edition Lesson B Summary Concatenation operator (&) ◦Used to link strings InputBox function ◦Displays interactive dialog box Use ControlChars.NewLine to move insertion point to a new line Set default button in form’s AcceptButton property ToString method ◦Formats number for string output 57

58 Programming with Microsoft Visual Basic 2010, 5 th Edition Lesson C Objectives After studying Lesson C, you should be able to: Include a Static variable in code Code the TextChanged event procedure Create a procedure that handles more than one event 58

59 Programming with Microsoft Visual Basic 2010, 5 th Edition Modifying the Load and Click Event Procedures Capability needed when each order is calculated ◦Order form should ask for the salesperson’s name Revise TOE chart before implementing changes Shift task of retrieving name to btnCalc’s Click event Use static variable for the salesperson’s name 59

60 Programming with Microsoft Visual Basic 2010, 5 th Edition 60 Figure 3-45 Revised TOE chart

61 Programming with Microsoft Visual Basic 2010, 5 th Edition Modifying the Load and Click Event Procedures (cont’d.) 61 Figure 3-46 Revised Pseudocode for the Calculate Order button

62 Programming with Microsoft Visual Basic 2010, 5 th Edition Coding the TextChanged Event Procedure TextChanged event ◦Occurs when the Text property value of a control changes Can occur when: ◦The user enters data into the control ◦Code assigns data to the control’s Text property Example: ◦A change is made to the number of items ordered 62

63 Programming with Microsoft Visual Basic 2010, 5 th Edition Coding the TextChanged Event Procedure (cont’d.) Associating a procedure with different objects and events ◦Handles clause ◦Appears in an event procedure’s header ◦Indicates object and event associated with procedure Can associate an event procedure with more than one object and/or event ◦In Handles section of procedure header, list each object and event, separated by commas 63

64 Programming with Microsoft Visual Basic 2010, 5 th Edition Coding the TextChanged Event Procedure (cont’d.) 64 Figure 3-48 Completed ClearLabels procedure

65 Programming with Microsoft Visual Basic 2010, 5 th Edition 65 Figure 3-49 Playtime Cellular application’s code at the end of Lesson C (continues)

66 Programming with Microsoft Visual Basic 2010, 5 th Edition 66 Figure 3-49 Playtime Cellular application’s code at the end of Lesson C (cont’d.)

67 Programming with Microsoft Visual Basic 2010, 5 th Edition Lesson C Summary TextChanged event procedure responds to change in value of control’s Text Property Handles clause ◦Determines which objects and events are associated with the event procedure To create procedure for more than one object or event: ◦List each object and event after Handles keyword 67


Download ppt "Programming with Microsoft Visual Basic 2010 5 th Edition CHAPTER THREE USING VARIABLES AND CONSTANTS."

Similar presentations


Ads by Google