A Little Coding Baby ...
7. The Code Editor - continued ...
Let's move on with our coding. From the Window1 Code Editor (which you know how to get to now right ? ), close the Controls Handler (by clicking the arrow), and click on the Events Handler arrow. Click on the Open Event.
These Events are associated with our main application window, window1. Most of these Events do exactly what you would expect them to do. For instance, the Close Event. If you wanted your application to do something as it (the window) was closing (perhaps a nice "Thank You !" message), you would tell it to do so under the close event. Learning what these various events are and which one you choose to place your code in is matter of experience and trial and error (hacking).
Our first task to duplicate from Chapter 5 is to rename the title of our window from "Untitled" to "My First Project". The most obvious time (or event) for this to occur is as our window begins to open. This happens in the twinkling of eye before we actually see the window on our computer screen.
Click on the "Open" event. The word "Sub" stands for subroutine. A subroutine is a fancy word for "do this during this particular event". These subroutines are the instructions we are giving to our project and these messages must be instructions that RB understands. Our code is typed between "Sub(Open)" "End Sub" and it is up to us as beginners to become familiar with the language that RB knows and understands.
A powerful technique for writing code for beginners and advanced programmers alike, is to state what you want to happen in plain English.
Simple English Code:
Be reminded that there are other lines of code and techniques that can be written to get RB to do what you want it to do. In other words, there is more than one way to skin a cat and there is more than one way to write code. This might explain some of the "attitude" you get from experienced programmers = ) .
- I would like to change the windows title to "My First Project".
Does RB understand this ? No it does not, but you do, and it gets us going. Our first line of code is actually no code at all, but our Comment to ourselves.
Commenting your code is important. You are forgetful and when you look back at your project, you will forget many details. Commenting your code makes your project orderly and crystal clear to you and anybody else who might be reading your master piece.
- In the Open Event of the window type this:
// I would like to change the windows title bar to
// read "My First Project"
The two back slashes that start each line simply tell the RB compiler, "RB dude, this is not for you, so skip over it !". Notice too that if you write comments on the next line you need to add two more back slashes.
- Here is what you are going to type below our comment:
Window1.Title="My First Project"
This tells (commands) RB in a language it understands (code) to replace the title property of window1 with this string of words. The syntax / code starts with the target (Window1) name for our instructions. We use the "Dot" notation then the property name (Title). The equals sign is referred to as an operator and the quote marks indicate the string of words we want to appear in the title bar of our window. To be proper; the operator assigns the string value, My First Project, to the title property of the window.
Please, at this point, run your program (Command-R) and enjoy your handy work ! Everything run OK ? Any problems to clear up ? Try this before moving on. An Exercise
If not our little exercise continues in:
Your REALbasic (RB) Learning Library and Reminders
- An Event
I talk about an Event in this section and a bit of an explanation is in order. Opening up your toolshed is an event. Picking up your hammer is another and driving in a nail with the hammer still another. Clicking your mouse, moving your mouse, starting up your computer, typing on your keyboard and quiting an application are all examples of events.
REALbasic and your Mac are keeping track of these events for you. They are doing this behind the scenes. In fact it has been said that all a computer ever does is monitor what events you are doing and responding accordingly.
- An Algorithm
Yikes there's a scary word!. But not worry it is not. Events and Algorithms are closely associated. Another analogy will help. Can you describe in detail to a robot how to fry an egg? How about this: go to the refrigerator. If there is an egg carton, take out two eggs. Return the egg carton to refrigerator. Grab a frying pan. Place oil in frying pan. Crack open egg in to frying pan. Etc, etc. etc. These sets of instructions and the order that they are done in, is an algorithm, a recipe. An ordering of events that accomplish a task. Simple enough. But, I'm sure you could easily expand and improve upon my little egg frying routine. Just think of all of the things that could go wrong if you didn't anticipate all of the little things that our robot friend might encounter as he was trying to fry an egg. Gosh did I mention the stove?
That my friends is what a great program does, it anticipates what the user might do and responds accordingly. This is the blend of skill, experience and the art of computer programming !
- An Exercise
After you have completed this chapter, change your code to read:
Window1.Tytle="My First Project"
* All on one line ! *
Run Your project. I am purposely getting you to type in an error. RB informs you of an "Unknown Identifier" error. RB does not understand the word "Tytle" and it can not compile and run the project until it understands everything you have told it to do.