Tip Type Conversion Functions

To find out more about the CSng function (and other type conversion functions), position the insertion point anywhere within the word CSng and press F1.

The next instruction, allPurchase = allPurchase + purchCost, adds to the current purchase value the new value supplied by the InputBox function. When you run this procedure for the first time, the content of the allPurchase variable is the same as the content of the purchCost variable. When you run the procedure the second time, the value of the static variable is increased by the new value supplied in the dialog box. You can run the CostOfPurchaseprocedure as many times as you want. The allPurchase variable will keep the running total for as long as the project is open. To try out this procedure, follow these steps:

1. Position the cursor anywhere within the CostOfPurchase procedure and press F5.

2. When the dialog box appears, enter a number. For example, enter 100 and press Enter. Visual Basic displays the message "The cost of a new purchase is: 100."

3. Click OK in the message box. Visual Basic displays the second message "The running cost is: 100."

4. Rerun the same procedure. When the input dialog appears, enter another number. For example, enter 50 and press Enter. Visual Basic displays the message "The cost of a new purchase is 50."

5. Click OK in the message box. Visual Basic displays the second message "The running cost is: 150."

6. Run the procedure several more times to see how Visual Basic keeps track of the running total.

0 0

Post a comment