In this exercise, you learn how to specify that one or more arguments are optional in a Call statement. Open the Optional Solution (Optional Solution.sln) file contained in the VB2010\Chap07\ Optional...


In this exercise, you learn how to specify that one or more arguments are optional in a Call statement. Open the Optional Solution (Optional Solution.sln) file contained in the VB2010\Chap07\ Optional Solution folder. If necessary, open the designer window.


a. Open the Code Editor window and review the existing code. The btnCalc control’s Click event procedure contains two Call statements. The first Call statement passes three variables to the CalcBonus procedure. The second call statement, however, passes only two variables to the procedure. (Do not be concerned about the jagged line that appears below the second Call statement.) Notice that the dblRate variable is omitted from the second Call statement. You indicate that the dblRate variable is optional in the Call statement by including the keyword Optional before the variable’s corresponding parameter in the procedure header; you enter the Optional keyword before the ByVal keyword. You also assign a default value that the procedure will use for the missing parameter when the procedure is called. You assign the default value by entering the assignment operator and the default value after the corresponding parameter in the procedure header. In this case, you will assign the number .1 as the default value for the dblRate variable. (Optional parameters must be listed at the end of the procedure header.)


b. Change the ByVal dblBonusRate As Double in the procedure header appropriately. Save the solution and then start the application. Enter a and 1000 in the Code and Sales boxes, respectively. Click the Calculate button. Type .05 and press Enter. The Call CalcBonus(dblSales, dblBonus, dblRate) statement calls the CalcBonus procedure, passing it the number 1000, the address of the dblBonus variable, and the number .05. The CalcBonus procedure stores the number 1000 in the dblTotalSales variable. It also assigns the name dblBonusAmount to the dblBonus variable and stores the number .05 in the dblBonusRate variable. The procedure then multiplies the contents of the dblTotalSales variable (1000) by the contents of the dblBonusRate variable (.05), assigning the result (50) to the dblBonusAmount variable. The lblBonus.Text = dblBonus.ToString("C2") statement then displays $50.00 in the lblBonus control.


c. Now enter b and 2000 in the Code and Sales boxes, respectively. Click the Calculate button. The Call CalcBonus(dblSales, dblBonus) statement calls the CalcBonus procedure, passing it the number 2000 and the address of the dblBonus variable. The CalcBonus procedure stores the number 2000 in the dblTotalSales variable and assigns the name dblBonusAmount to the dblBonus variable. Because the Call statement did not supply a value for the dblBonusRate parameter, the default value (.1) is assigned to the variable. The procedure then multiplies the contents of the dblTotalSales variable (2000) by the contents of the dblBonusRate variable (.1), assigning the result (200) to the dblBonusAmount variable. The lblBonus.Text = dblBonus. ToString("C2") statement then displays $200.00 in the lblBonus control. Close the Code Editor window and then close the solution.

Nov 14, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here