Question: 18. In this exercise, you experiment with procedure-level and class-level variables. Open the Scope Solution (Scope Solution.sln) file contained in the VB2012Chap03Scope Solution folder. The
18. In this exercise, you experiment with procedure-level and class-level variables. Open the Scope Solution (Scope Solution.sln) file contained in the VB2012\Chap03\Scope Solution folder. The Scope application allows the user to calculate either a 5% or 10%
commission on a sales amount. It displays the sales and commission amounts in the lblSales and lblCommission controls, respectively.
a. Open the Code Editor window and then open the code template for the btnSales control’s Click event procedure. Code the procedure so that it declares a variable named dblSales. The procedure also should use an assignment statement to assign the number 500 to the variable. In addition, the procedure should display the contents of the variable in the lblSales control on the form.
b. Save the solution and then start the application. Click the Display Sales button.
What does the button’s Click event procedure display in the lblSales control? When the Click event procedure ends, what happens to the dblSales variable? Click the Exit button.
c. Open the code template for the btnComm5 control’s Click event procedure. In the procedure, enter an assignment statement that multiplies a variable named dblSales by .05, assigning the result to the lblCommission control. When you press the Enter key after typing the assignment statement, a jagged line appears below dblSales in the instruction. The jagged line 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 control’s Click event procedure cannot locate the variable’s declaration statement, which you previously entered in the btnSales control’s Click event 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 control’s Click event 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 jagged line disappears from the assignment statement. Save the solution and then start the application.
Click the Display Sales button. The contents of the dblSales variable declared in the btnSales control’s Click event 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 control’s Click event 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 procedure-level 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. Click the blank line above the btnExit control’s Click event procedure. The Class Name and Method Name boxes show frmMain and (Declarations), respectively. Press Enter to insert a blank line. In the blank line, enter a statement that declares a class-level variable named dblSales.
f. Delete the Dim statement from the btnSales control’s Click event procedure. Also delete the Dim statement from the btnComm5 control’s Click event procedure.
g. Open the code template for the btnComm10 control’s Click event procedure. In the procedure, enter an assignment statement that multiplies the dblSales variable by .1, assigning the result to the lblCommission control.
h. Save the solution and then start the application. The variable declaration statement in the form’s Declarations section creates the dblSales variable and initializes it to 0.
Click the Display Sales button. The button’s Click event procedure stores the number 500 in the dblSales variable and then displays the contents of the variable (500) in the lblSales control. Click the 5% Commission button. The button’s Click event procedure multiplies the contents of the dblSales variable (500) by .05 and then displays the result (25) in the lblCommission control. Click the 10% Commission button. The button’s Click event procedure multiplies the contents of the dblSales variable (500) by .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? Close the Code Editor window and then close the solution.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
