Visual Basic Tutorial - Part 5
In this section, we're going to run over some old turf. I'll be skimming over a few key topics we covered during the past few tutorials to ensure you're all keyed up for the "function" leap.
Confused by methods, events and properties?
If you get bored, that's fine. Just take a quick walk. Or go swimming. Or try hitting a fellow programmer. Or throw your computer out of the window. Or try asking a foreigner for technical support (ps. undertaking any of these actions may result in a serious health decrease).
I've blabbled on about objects and their properties and events before - and have lightly touched on "methods" - but we've never really explained them in great depth. In this section we'll be running these real-life programming scenarios alongside an interesting (and rather tasty) analogy that will have you crying "eureka!" within the hour.
You can liken programming objects to real-life things. I know you like pretending, so let's imagine an orange in a nerd's world. Each orange would have properties, methods and events.
One such property of the orange could be its colour. The default would probably be orange, but in the wonderful Windows world, you'd be able to change that with a small piece of code like:
Orange.Colour = Green
Wow! Imagine the dollars you could earn selling novelty green oranges!
You can read and write most properties in code - in other words, you can set their properties using code similar to the above - or read them in code like:
MsgBox "The colour of your orange is " & Orange.Colour
Other properties of the orange may include Size (measured on a scale of 1 to 10) or Poisonous (True of False). We've dealt with properties before. Remember when we set the Caption, Font and Name properties of our command buttons?
The orange also has "methods". A method is just something that happens. Remember we talked about subs in the last tutorial? A method is just a sub that refers to this particular object. So, your juicy orange may have a "Slice" method or an "Eat" method. To run such a method, you simply use the code:
Make sense? Hope so. We've not encountered many object methods to date - but let's take a few examples. You may be creating a spreadsheet like program and use the method:
In brief, a method is just a piece of code you fire off that often affects the object you're working with.
And last, but certainly not least are events. Events are little triggers that tell your program when something has happened. So you might put the orange into your Visual Basic program - and you need to know when the user takes a bite, so you can add it onto their current bill.
How do you do it? You simply respond to an event. We've used events before - remember we put code under the Click event of our command button to display a message box? That's all an event is. When something happens in the object, it tells Visual Basic - and you can throw a bit of code together to respond to that particular event if you wish.
Your code usually looks something like this:
Private Sub Command1_Click() MsgBox "You clicked me! Don't do that again, sonny!" End Sub
The first "Private Sub" line and closing "End Sub" are automatically added by Visual Basic when you select the event from the code window. They simply define the code boundaries.
The code for our orange probably looks something like this:
Private Sub Orange_TookABite() MsgBox "I know you've just taken a bite!" Msgbox "This code runs whenever the TookABite event of the Orange objects fires up!" End Sub
Likewise we could add code to respond when a user clicks on a command button or changes the value of a text box - just by entering code under the appropriate event.
I hear you crying "Eureka!" at this point. You've finally figured out what I've been boring you with over the past few weeks. Why, I hear you cry, have I saved this simplified analogy from you for so long? Two reasons -
- (a) Because the comparison is too simple for you to grasp during those early days - without a proper hands-on look at what's happening behind the scenes
- (b) Because I enjoyed watching you sweat the past four tutorials
- (c) Because I'm a small, evil man with a horrid cackle and an in-built urge to rule the world. Hahaha! Hahahahahaha! HAHAHAHAHAHAHAHA!!! Ohh. Sorry. Errm, yes. Sorry about that. Must learn to control myself in public
[Ed: That's three reasons, Karl!]
Anyway, enough of that. Lets move onto "functions" - a really, really cool thingy in Visual Basic.
Page 2 of 5