A Word About Objects
When learning about Small Basic, you’ll see the words “object” and “method” very often. So we’ll start with a definition of these terms.
Imagine you’re getting ready to watch your favorite TV show after an exhausting day at school. You put a popcorn bag in the microwave and press a button labeled Popcorn on the microwave’s front panel. After a couple of minutes, the microwave beeps to alert you that it has completed its task.
The microwave is an example of an object. This object is capable of performing predefined tasks. We call these tasks methods. You tell the Microwave object to execute its Popcorn() method by pressing the method’s button on the interface. In programming terms,
we say that you “called (or invoked) the Popcorn() method of the Microwave object,” and we write this as:
You can view this statement as a command to the Microwave object to execute its Popcorn() method. Notice the dot between the object name and the method name.
Let’s now assume also that you wanted to heat up a cup of milk to make hot chocolate. You look at the microwave’s front panel for an appropriate button, but you don’t find one. So, you press the button labeled “2 minutes” followed by the Start button. Here, you have called the Start() method of the Microwave object, and you gave it a number (2 minutes in this case) to tell it for how long it’s supposed to work. In programming terms, we can write this command to the Microwave as:
Start() is another method of the Microwave object. But this method needs an input that tells it exactly what you intend to do (in this case, how long for the microwave to run). The input to the method (the number 2 in this case) is called a parameter or argument.
Objects in the Small Basic library work the same way. The designers of the library created some “software” objects (i.e., they wrote some code) and gave these objects representative names. For example, they created an object called Dictionary that supports two methods. One of the methods is called GetDefinition(). If you want to see the definition of the word “computer”, for example, you should write:
The word “computer” is a parameter that we passed to the GetDefinition() method, which belongs to the Dictionary object.
NOTE: We include parenthesis with the names of the methods, such as GetDefinition(), to make them easier to recognize.
Suppose that you have an object called TV that has a method called SetChannel(). This method takes one argument – a number that specifies the desired channel.
What would you write to invoke the SetChannel() method to switch the TV to channel 20?
Reply in the comments with any clarifying questions.
Click here to download and learn more about Small Basic: http://blogs.msdn.com/b/smallbasic/
Have a Small and Basic Day!
– Majed Marji & User Ed