Programming with Microsoft Visual Basic 2015 Chapter 3

Programming with Microsoft Visual Basic 2015 Chapter 3: Using Variables and Constants © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.

Previewing the Modified Bakery Application • Meyer’s Purple Bakery application – New screen for salesclerk’s name – Sales tax added to order Figure 3 -1 Name Entry dialog box Figure 3 -2 Completed sales receipt Programming with Microsoft Visual Basic 2015 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 2

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 Try. Parse method • Convert numeric data to a different data type using the Convert class methods • Explain the scope and lifetime of variables and named constants • Explain the purpose of Option Explicit, Option Infer, and Option Strict Programming with Microsoft Visual Basic 2015 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 3

Using Variables to Store Information • Controls and variables temporarily store data • Variable – A temporary storage location in main memory – Specified by data type, name, scope, and lifetime • Reasons to use variables – To hold information that is not stored in a control on the form – To allow for more precise treatment of numeric data – To enable code to run more efficiently Programming with Microsoft Visual Basic 2015 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 4

Using Variables to Store Information (cont. ) Selecting a Data Type for a Variable • Data type – Specifies the type of data a variable can store – Provides a class template for creating variables • Unicode – A universal coding scheme for characters – Assigns a unique numeric value to each character in the written languages of the world Programming with Microsoft Visual Basic 2015 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 5

Using Variables to Store Information (cont. ) Figure 3 -3 Basic data types in Visual Basic Programming with Microsoft Visual Basic 2015 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 6

Using Variables to Store Information (cont. ) • The textbook uses: – The Integer data type for all integers – Either the Decimal or Double data type for numbers containing decimal places or numbers used in calculations – The String data type for text or numbers not used in calculations – The Boolean data type for Boolean values Programming with Microsoft Visual Basic 2015 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 7

Using Variables to Store Information (cont. ) Selecting a Name for a Variable – Names must begin with a letter or underscore Figure 3 -4 Three-character IDs and examples – Names can contain only letters, numbers, or underscores • No punctuation, special characters, or spaces are allowed – The recommended length for a name variable is 32 characters – Variable names cannot be reserved words (such as Sub or Double) Programming with Microsoft Visual Basic 2015 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 8

Using Variables to Store Information (cont. ) • Valid names: – int. Feb_Income – dec. Sales 2014 – dbl. East. Region – str. Name • Invalid Names: – 4 th. Quarter • The name must begin with a letter or underscore – dbl. West Region • The name cannot contain a space – str. First. Name • The name cannot contain punctuation – dec. Sales$East • The name cannot contain a special character Programming with Microsoft Visual Basic 2015 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 9

Using Variables to Store Information (cont. ) Declaring a Variable • Declaration statement – Used to declare (create) a variable and reserve space in memory for it • If no initial value is given to a variable when declaring it, the computer stores a 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 AM Programming with Microsoft Visual Basic 2015 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 10

Using Variables to Store Information (cont. ) Figure 3 -6 Syntax and examples of a variable declaration statement Programming with Microsoft Visual Basic 2015 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 11

Assigning Data to an Existing Variable • Assignment statement – Assigns a value to a variable at run time – Syntax: variablename = expression – An expression may include literal constants, object properties, variables, keywords, and arithmetic operators • Literal constant – A data item whose value does not change while the application is running – Example: The string “Mary” • Literal type character – Forces a literal constant to change its data type Programming with Microsoft Visual Basic 2015 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 12

Assigning Data to an Existing Variable (cont. ) Figure 3 -7 Syntax and examples of assigning a value to a variable during run time Programming with Microsoft Visual Basic 2015 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 13

Assigning Data to an Existing Variable (cont. ) The Try. Parse Method • Converts a string to a number • Is preferred over Val – Allows the programmer to specify the data type – Val only returns a Double number • Arguments – data. Type: – String: – Variable: A numeric data type, such as Integer A string to be converted A variable that receives the numeric value Programming with Microsoft Visual Basic 2015 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 14

Assigning Data to an Existing Variable (cont. ) Figure 3 -9 Basic syntax and examples of the Try. Parse method Programming with Microsoft Visual Basic 2015 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 15

Assigning Data to an Existing Variable (cont. ) Figure 3 -10 Results of the Try. Parse method for the Double, Decimal and Integer data types Programming with Microsoft Visual Basic 2015 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 16

Assigning Data to an Existing Variable (cont. ) The Convert Class • Can be used to convert a number from one type to another • Methods include To. Decimal, To. Double, To. Int 32, and To. String • Try. Parse is recommended for converting strings to numeric data types – Will not produce an error if the conversion fails Programming with Microsoft Visual Basic 2015 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 17

Assigning Data to an Existing Variable (cont. ) Figure 3 -11 Syntax and examples of the Convert class methods Programming with Microsoft Visual Basic 2015 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 18

The Scope and Lifetime of a Variable • Scope – Indicates where a variable can be used • Lifetime – How long a variable remains in memory • Scope and lifetime are determined by where a variable is declared: either the General Declarations section or the form’s Declaration section • Three types of scope: – Class: The variable can be used by all procedures in a form – Procedure: The variable can be used within a procedure – Block: The variable can be used within a specific code block Programming with Microsoft Visual Basic 2015 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 19

The Scope and Lifetime of a Variable (cont. ) • Variables with Procedure Scope – – Can be used only by that procedure Declared at the beginning of the procedure Removed from memory when the procedure ends Declared using the Dim keyword • Most variables used in this course will be procedurelevel variables Figure 3 -12 User Interface for the Commission Calculator application Programming with Microsoft Visual Basic 2015 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 20

The Scope and Lifetime of a Variable (cont. ) (continued Figure 3 -13 Click event procedures using procedure-level variables Programming with Microsoft Visual Basic 2015 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 21

The Scope and Lifetime of a Variable (cont. ) Figure 3 -14 Code Editor window for the Commission Calculator application Programming with Microsoft Visual Basic 2015 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 22

The Scope and Lifetime of a Variable (cont. ) • Variables with Class Scope – – Can be used by all procedures in the form Declared in the form’s Declarations section Will remain in memory until the application ends Declared using the Private keyword Figure 3 -16 User Interface for the Total Scores application Programming with Microsoft Visual Basic 2015 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 23

The Scope and Lifetime of a Variable (cont. ) Figure 3 -17 Most of the Total Scores application’s code using a class-level variable Programming with Microsoft Visual Basic 2015 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 24

Static Variables • Static variable – A procedure-level variable with an extended lifetime • Remains in memory between procedure calls • Retains its value even when the procedure ends – Static keyword • Used to declare a static variable – Static variables act like class-level variables but have narrower scope • They can only be used by the procedure in which they are declared Programming with Microsoft Visual Basic 2015 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 25

Static Variables (cont. ) Figure 3 -19 Most of Total Scores application’s code using a static variable Programming with Microsoft Visual Basic 2015 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 26

Named Constants • Named constant – A memory location inside the computer whose contents cannot be changed at run time • Const statement – Creates named constant – Stores value of expression in a named constant – expression: Can be a literal constant, another named constant, or an arithmetic operator – Cannot contain a variable or method Programming with Microsoft Visual Basic 2015 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 27

Named Constants (cont. ) Figure 3 -20 Syntax and examples of the Const statement Programming with Microsoft Visual Basic 2015 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 28

Named Constants (cont. ) Figure 3 -21 User interface for the Area Calculator application Figure 3 -22 Calculate Area button’s Click event procedure Programming with Microsoft Visual Basic 2015 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 29

Option Statements Option Explicit and Option Infer • Prevent you from using undeclared variables Option Strict • Implicit type conversion – Converts the right-side value to the data type on the left side • Promotion – Data is converted to a greater precision number (e. g. , Integer to Decimal) • Demotion – Data is truncated (e. g. , Decimal to Integer) – Data loss can occur when demotion occurs • Infer – Ensures that every variable is declared with a data type Programming with Microsoft Visual Basic 2015 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 30

Option Statements (cont. ) Option Strict (cont. ) – Disallows implicit conversions – Type conversion rules are applied when this option is on Figure 3 -24 Rules and examples of type conversions Programming with Microsoft Visual Basic 2015 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 31

Option Statements (cont. ) Figure 3 -25 Option statements entered in the General Declarations section Programming with Microsoft Visual Basic 2015 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 32

Lesson A Summary • • • Declare a variable using {Dim | Private | Static} An assignment statement assigns a value to a variable Three levels of scope: block, procedure, class The Try. Parse method 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 Programming with Microsoft Visual Basic 2015 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 33

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 Input. Box function • Include the Control. Chars. New. Line constant in code • Designate the default button for a form • Format numbers using the To. String method Programming with Microsoft Visual Basic 2015 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 34

Modifying the Meyer’s Purple Bakery Application • Modifications needed – Calculate and display the sales tax – Display salesperson’s name • • Revise the TOE chart to reflect the new tasks You must modify btn. Calc button’s Click event and the form’s Load event Programming with Microsoft Visual Basic 2015 Figure 3 -26 Revised TOE chart for the bakery application © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 35

Modifying the Meyer’s Purple Bakery Application (cont. ) Figure 3 -27 Bakery application’s modified interface] Programming with Microsoft Visual Basic 2015 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 36

Modifying the Calculate Button’s Code • General strategy – Remove the existing code from the Click event procedure – Recode the procedure using variables in equations • Use the Option Explicit On statement – Enforces full variable declaration • Use the Option Infer Off statement – Ensures that variables are declared with data types • Use the Option Strict On statement – Suppresses implicit type conversions Programming with Microsoft Visual Basic 2015 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 37

Modifying the Calculate Button’s Code (cont. ) Figure 3 -28 A red squiggle indicates a syntax error Figure 3 -29 Lines to delete from the procedure Programming with Microsoft Visual Basic 2015 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 38

Modifying the Calculate Button’s Code (cont. ) Figure 3 -30 Revised pseudocode and flowchart for the btn. Calc_Click procedure Programming with Microsoft Visual Basic 2015 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 39

Modifying the Calculate Button’s Code (cont. ) Figure 3 -31 List of named constants and variables for the btn. Calc_Click procedure Figure 3 -32 Const and Dim statements entered in the procedure Programming with Microsoft Visual Basic 2015 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 40

Modifying the Calculate Button’s Code (cont. ) Figure 3 -34 Calculated amounts shown in the interface Figure 3 -33 Code entered in the btn. Calc _Click procedure Programming with Microsoft Visual Basic 2015 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 41

Using the To. String Method to Format Numbers • Formatting – Specifying decimal places and special characters to display • The To. String method replaces the Format function • Syntax: variablename. To. String(format. String) – variablename: The name of a numeric variable – format. String: The string specifying the format you want to use • format. String must take the form Axx, where A is the format specifier and xx is the precision specifier Programming with Microsoft Visual Basic 2015 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 42

Using the To. String Method to Format Numbers (Cont. ) Figure 3 -35 Syntax and examples of the To. String method Programming with Microsoft Visual Basic 2015 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 43

Concatenating Strings • Concatenate – To connect strings together • Concatenation operator – The ampersand (&) – Include a space before and after the ampersand • Numeric values used with the & operator are converted to strings Figure 3 -37 Examples of string concatenation Programming with Microsoft Visual Basic 2015 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 44

Concatenating Strings (cont. ) Figure 3 -38 String concatenation included in the assignment statement Figure 3 -39 Concatenated strings displayed in the lbl. Msg control Programming with Microsoft Visual Basic 2015 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 45

The Input. Box Function Input. Box function – Displays an input dialog box and retrieves user input • Arguments – prompt: Contains the message to display inside the dialog box – title: Controls the text that appears in the dialog box’s title bar – default. Response: Controls the text that appears in the input field • The returned value most often assigned to String variable • The syntax is shown in Figure 3 -41 on the next slide Programming with Microsoft Visual Basic 2015 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 46

The Input. Box Function (cont. ) Figure 3 -40 Example of an input dialog box Figure 3 -41 Basic syntax and examples of the Input. Box function Programming with Microsoft Visual Basic 2015 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 47

The Input. Box Function (cont. ) Figure 3 -42 Class-level variable declared in the form’s Declarations section Figure 3 -43 frm. Main Load procedure Figure 3 -44 Dialog box created by the Input. Box function Programming with Microsoft Visual Basic 2015 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 48

The Control. Chars. Newline Constant Control. Chars. New. Line constant – Advances the insertion point to the next line in a control – Also used to advance the insertion point in file or on the printer • To use, type Control. Chars. New. Line in the appropriate location – Can be used with string concatenation • Line continuation character (_) – Used to break up a long line of code into two or more lines Programming with Microsoft Visual Basic 2015 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 49

The Control. Chars. Newline Constant (cont. ) Figure 3 -45 Modified assignment statement Figure 3 -47 Salesclerk’s name shown on the sales receipt Programming with Microsoft Visual Basic 2015 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 50

Designating a Default Button Default button – Activated by pressing the Enter key – Not required to have the focus – Only one per form • The default button should be the button used most often by the user – Except if button’s task is destructive and irreversible, such as deleting data • Set the form’s Accept. Button property to the button name Programming with Microsoft Visual Basic 2015 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 51

Designating a Default Button (Cont. ) (continued) Figure 3 -48 Bakery application’s code at the end of Lesson B (continues) Programming with Microsoft Visual Basic 2015 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 52

Designating a Default Button (Cont. ) (continued) Figure 3 -48 Bakery application’s code at the end of Lesson B Programming with Microsoft Visual Basic 2015 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 53

Lesson B Summary • The concatenation operator (&) is used to link strings • The Input. Box function displays an interactive dialog box • Use Control. Chars. New. Line to move the insertion point to a new line • Set the default button in the form’s Accept. Button property • The To. String method formats a number for string output Programming with Microsoft Visual Basic 2015 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 54

Lesson C Objectives After studying Lesson C, you should be able to: • Include a static variable in code • Code the Text. Changed event procedure • Create a procedure that handles more than one event Programming with Microsoft Visual Basic 2015 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 55

Modifying the Load and Click Event Procedures Figure 3 -50 Revised TOE Chart for the bakery application in Lesson C Programming with Microsoft Visual Basic 2015 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 56

Modifying the Load and Click Event Procedures (Cont. ) Figure 3 -51 Revised pseudocode for the Calculate button in Lesson C Programming with Microsoft Visual Basic 2015 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 57

Coding the Text. Changed Event Procedure • Text. Changed event – Occurs when the Text property value of a control changes • Can occur when: – The user enters data into the control – The code assigns data to the control’s Text property • Example: A change is made to the number of items ordered Programming with Microsoft Visual Basic 2015 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 58

Coding the Text. Changed Event Procedure (Cont. ) Associating a Procedure with Different Objects and Events • Handles clause – Appears in an event procedure’s header – Indicates the object and event associated with the procedure • You can associate an event procedure with more than one object and/or event – In the Handles section of procedure header, list each object and event separated by commas – Procedure names are entered in Pascal case • Capitalize the first letter in the name and the first letter of each subsequent word in the name Programming with Microsoft Visual Basic 2015 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 59

Coding the Text. Changed Event Procedure (cont. ) Figure 3 -52 Text. Changed event procedure associated with the txt. Donuts control Figure 3 -53 Completed Clear. Labels procedure Programming with Microsoft Visual Basic 2015 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 60

Coding the Text. Changed Event Procedure (cont. ) Figure 3 -54 Completed sales receipt Programming with Microsoft Visual Basic 2015 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 61

Lesson C Summary • The Text. Changed event procedure responds to a change in the value of a control’s Text Property • The Handles clause determines which objects and events are associated with the event procedure • To create a procedure for more than one object or event, list each object and event after the Handles keyword Programming with Microsoft Visual Basic 2015 © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 62
- Slides: 62