Part One Part Two Part Three Part Four (a) |
This time, let me take you under the rippling surface of Hex’s messages with a subroutine.
To give you some idea of what my unsuitable analogy is getting at, a subroutine is a small piece of programming that you can use again and again, without having to type it in every time.
First of all, let me show you the new, expanded, program. As previously, paste this into your text editor, save it with a .vbs file extension and see what happens when you run it.
HEX
Sub HEX()
x=msgbox(“+++ Error At Address: 14, Treacle Mine Road, Ankh-Morpork +++”,2+16+256,”HEX”)
If x = 3 Then
ABORT
ElseIf x = 4 Then
RETRY
ElseIf x = 5 Then
IGNORE
End If
End Sub
Sub ABORT()
x=msgbox(“+++ No, You Abort +++”,0+32+0,”HEX”)
End Sub
Sub IGNORE()
x=msgbox(“+++ ???The Silent Treatment??? +++”,0+32+0,”HEX”)
End Sub
Sub RETRY()
HEX
End Sub
I’ve removed the Randomize function and only displayed one message in the main part of the program this time, to make it a bit simpler. You turn a piece of program into a subroutine by surrounding it with the “Sub” code, something like this:
Sub <SUBROUTINE_NAME>()
<Code for the subroutine to carry out>
End Sub
Referencing the above program, I created a messagebox saying there is an (“+++ Error At Address: 14, Treacle Mine Road, Ankh-Morpork +++”, with Abort, Retry and Cancel buttons. The next three sections are subroutines, telling the script what to do when each button is pressed. So if the Abort button is pressed:
If x = 3 Then
ABORT
The figure returned by the messagebox (x) is 3 and the program needs to find and run (or “call”) the subroutine called “ABORT”:
Sub ABORT()
x=msgbox(“+++ No, You Abort +++”,0+32+0,”HEX”)
End Sub
When the Abort button is pressed another messagebox is displayed saying “+++ No, You Abort +++”.
The observant amongst you might have noticed that the program starts off by calling the HEX subroutine, which surrounds the main body of the code displaying the Treacle Mine Road text and deciding what to do when different buttons are pressed. The Final subroutine “RETRY” also calls HEX, which means that clicking Retry will show the main message again:
Sub RETRY()
HEX
End Sub
In simple terms:
When RETRY is called within our program
Run HEX
That’s the subroutine finished
Therefore, whenever you need to run any of the parts of the program you can call them using the HEX, IGNORE, RETRY and CANCEL subroutines. Makes life much simpler!
If any of this sounds like gibberish, you might want to go back to Part One for a refresher, or I might just be talking nonsense!!!
Next time we’ll try using external stimuli, e.g. what month it is, to decide on different behaviours.
Leave a comment