Question
In this exercise, you experiment with procedure-level and class-level variables. Open the VB2015Chap03Scope SolutionScope Solution (Scope Solution.sln) file. The application allows the user to calculate
a. Open the Code Editor window and then open the code template for the btnSales_ Click procedure. Code the procedure so that it performs the following three tasks: declares a variable named dblSales, uses an assignment statement to assign the number 500 to the variable, and displays the contents of the variable in the lblSales control.
b. Save the solution and then start the application. Click the Display Sales button. What does the btnSales_Click procedure display in the lblSales control? When the procedure ends, what happens to the dblSales variable? Click the Exit button.
c. Open the code template for the btnComm5_Click procedure. In the procedure, enter an assignment statement that multiplies a variable named dblSales by 0.05, assigning the result to the lblCommission control. When you press Enterafter typing the assignment statement, a red squiggle appears below dblSales in the instruction. The red squiggle indicates that the code contains a syntax error. To determine the problem, rest your mouse pointer on the variable name, dblSales. The message in the box indicates that the variable is not declared. In other words, the btnComm5_Click procedure cannot locate the variables declaration statement, which you previously entered in the btnSales_Click procedure. As you learned in Lesson A, only the procedure in which a variable is declared can use the variable. No other procedure is even aware that the variable exists.
d. Now observe what happens when you use the same name to declare a variable in more than one procedure. Insert a blank line above the assignment statement in the btnComm5_Click procedure. In the blank line, type a statement that declares the dblSales variable, and then click the assignment statement to move the insertion point away from the current line. Notice that the red squiggle disappears from the assignment statement. Save the solution and then start the application. Click the Display Sales button. The value stored in the dblSales variable declared in the btnSales_Click procedure (500) appears in the lblSales control. Click the 5% Commission button. Why does the number 0 appear in the lblCommission control? What happens to the dblSales variable declared in the btnComm5_Click procedure when the procedure ends? Click the Exit button. As this example shows, when you use the same name to declare a variable in more than one procedure, each procedure creates its own procedurelevel variable. Although the variables have the same name, each refers to a different location in memory.
e. Next, you use a class-level variable in the application. Insert a blank line below the Public Class clause. Enter a statement that declares a class-level variable named dblSales.
f. Delete the Dim statement from the btnSales_Click procedure. Also delete the Dim statement from the btnComm5_Click procedure.
g. Open the code template for the btnComm10_Click procedure. In the procedure, enter an assignment statement that multiplies the dblSales variable by 0.1, assigning the result to the lblCommission control.
h. Save the solution and then start the application. The variable declaration statement in the forms Declarations section creates the class-level dblSales variable and initializes it to 0. Click the Display Sales button. The buttons Click event procedure stores the number 500 in the class-level variable and then displays the contents of the variable (500) in the lblSales control. Click the 5% Commission button. The buttons Click event procedure multiplies the contents of the class-level variable (500) by 0.05 and then displays the result (25) in the lblCommission control. Click the 10% Commission button. The buttons Click event procedure multiplies the contents of the class-level variable (500) by 0.1 and then displays the result (50) in the lblCommission control. As this example shows, any procedure in the form can use a class-level variable. Click the Exit button. What happens to the class-level dblSales variable when the application ends?
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started