When Subs Arent Enough

Everything going well? Understand it all so far? Oh good - 'cause I'm about to throw a massive spanner into the works. You see, sometimes simple Subs just aren't clever enough.

Are standard subs too simple?

A more advanced form of procedure...

For instance, say you were creating an insurance quote system where you need to pass information to the Sub - say, the premium to pay each year. Then the Sub needs to multiply the premium by two and display the information in a message box.

Following me? Good. Create a new Standard Exe with a simple form and two text boxes and command buttons as such:

If you can remember doing maths at school, you'll probably recall that most fashionable of topics, algebra. Well, it's back in fashion.

With this code, we're telling Visual Basic that when we call "CalculateAmount" in the future, we'll also be passing it a parameter called "Amount" - the value of which we are currently unaware.

In the code, our Sub refers to this "Amount" - even though its value isn't currently known. The code displays the "Amount" in a message box - as well as multiplying the number by two with the code, "Amount * 2".

We've not talked about calculating in Visual Basic yet, but it's pretty straightforward. The calculation operators are determined by the following keys

Multiplication *

Addition +

Minus -

Division /

So if you wanted to add 5 and 7 together, then divide the result by 10 - you could program "(5+7)/10". If you've ever used Microsoft Excel, you'll know all of this.

What did you say? Shut up and get on with the tutorial? Oh. Yes, right. Fine. No problem. Don't worry over me. Lil' old me. That's fine.

Let's imagine we called our Sub and passed it the number four. It would take that "Amount" and display a box that should say something like:

"4 times two equals 8"

To calculate the 8, VB simply multiplies the value of "Amount" by two.

Let's add the code to call this Sub. Under the Click event of cmdCalcOne, add the code:

CalculateAmount(txtCalcOne.Text)

We're now calling the CalculateAmount Sub, passing it the text value of txtCalcOne as "Amount". If it's a number, it will calculate the appropriate figure.

In the Click event of cmdCalcTwo, add the code:

CalculateAmount(txtCalcTwo.Text)

Notice that we're now passing a different value to the Sub - that of our second text box.

Try running the application, entering various numbers into the text boxes and clicking their associated buttons.

If you don't enter a valid number into the boxes, be sure to expect errors. Don't say I didn't warn you! If you get errors such as "Type Mismatch" just hit the End button and start again. It's just warning you it can't calculate because you didn't enter a proper number.

Are you starting to see the value of Subs, particularly Subs with parameters? You pass this general piece of code a value that it utilises in code.

Perhaps you could use it to change the caption of your form, depending on the customer name. Perhaps you could use that one piece of code to disable or enable all the command buttons on your screen. Perhaps you could just forget about Visual Basic and go back to farming. It's your call.