Chapter #4: Methods.
A method is somewhat like an event. It is a function that is contained by the object that will receive the method. This functions are part of what the class of wich this control is. For example, all Forms accept the method 'Move', but command buttons also accept the method 'Move'... (weīll be playing with it in a minute). However, Forms accept the method 'Show', and command buttons donīt.
Letīs use our coffeepot and coffecup to illustrate how a method works:
A method always refers to the object that owns it, and can sometimes apply itīs action on other controls too.
|
Private Sub StartPouringTheCoffee() Do While Cup1.Full = False 'keep pouring until the cup is full CoffeePot1.pour(Cup1) 'apply the pour method from coffepot1 on cup1 Loop MsgBox "Coffe is Served!", vbExclamation, "Work done" 'gives the user a message End Sub |
If you are 'reaaally' new to vb and think that this 'while cup1.full = false' is ridiculous because it looks too much like plain english, and computer programming isnīt such, well, let me give you good news... parts of what formerly was the core of BASIC is still on VB, but some things got a litle make-up to let them look fresher (more 'developer-friendly'). For example, the built-in boolean values (True/False). You can test propertyies from objects that return boolean values to know if they are 'True' or 'False'.
Now, letīs play a litle with the 'real thing':
Weīll be using the 'move' method to move buttons around the screen, it will look as if the buttons were one jumping over the other. In other words, there will be only 1 form (form1 - the default) and two commandbuttons (command1 & command2).
Iīll give you commented code so you can understand it:
|
'---------8<------------- Private Sub Form_Load() 'This will execute when the program starts, upto the next 'End Sub'. Command1.Top = Form1.Top Command1.Left = Form1.Left Command2.Top = Form1.Top Command2.Left = Command1.Left + Command1.Left 'This will arrange the buttons so they donīt overlap each other, and at start, they are where we want them. End Sub Private Sub Command1_Click() 'This will execute when the button 'command1' is clicked Dim a 'Creates a variable a = Command2.Left + Command2.Width 'Stores the position and width of command2 on a Command1.Move(a) 'Moves command1 to the 'a' position. End Sub Private Sub Command2_Click() 'This will execute only when the button 'command2' is clicked Dim b 'Creates a variable b = Command1.Left + Command1.Width 'Stores the position and width of command1 on b Command2.Move(b) 'Moves command2 to the 'b' position. End Sub '-------8<----------- |
If youīve already tryied this code, you now know how a method is invoked and how it works.
A litle word about objects, controls, and the such:
There are millions of objects/controls out there. re-newed buttons, titlebars, statusbars, shell-extensions, internet-related controls, etc. With your copy of Visual Basic you get a few more objects than what you just see there on screen. For example, if you want to make an application using internet-related technology, you can use the 'Winsock OCX control'. To add a separate control you can 'right-click' on the 'Tools Panel'. The left-most one with the 'General' label on it. A window will popup with a list of controls currently installed (and registered on the registry) on your system. You can use any of these on your programs. All you have to do is know their propertyies, events, and methods. There is a lot of support, examples, projects and other resources you can find on internet, so donīt be afraid on trying them.
But there is a set of controls that is stuck there by default. You canīt take them out of your project. These controls (labels, command buttons, frames, pictureboxes, etc) are known as constituitive controls. They are located on the vbrun500.dll (for vb5), and go every where your program goes. You shouldnīt mind about them, because they are small, fast and very reliable, and what you donīt use wonīt add more 'weight' to your app.
Well, hope you enjoyed it and are ready to code up the world!
DrNo.