In this exercise, you learn how to specify that one or more arguments are optional in a

Question:

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

a. Open the Code Editor window and review the existing code. The btnCalc_Click 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. Notice that the dblRate variable is omitted from the second Call statement. You indicate that a 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 parameter. In this case, you will assign the number 0.1 as the default value for the dblRate variable. Make the appropriate changes to the ByVal dblBonusRate As Double statement in the procedure header.

b. Save the solution and then start the application. Enter the letter a and the number 1000 in the Code and Sales boxes, respectively. Click the Calculate button, and then 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. Next, enter the letter b and the number 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 (0.1) is assigned to the variable. The procedure then multiplies the contents of the dblTotalSales variable (2000) by the contents of the dblBonusRate variable (0.1), assigning the result (200) to the dblBonusAmount variable. The lblBonus.Text = dblBonus.ToString("C2") statement then displays $200.00 in the lblBonus control. Stop the application. Close the Code Editor window and then close the solution.

Fantastic news! We've Found the answer you've been seeking!

Step by Step Answer:

Related Book For  book-img-for-question
Question Posted: