| |||||||||||||||||||||||||||||||
|
| |||||||||||||||||||||||||||||||
Print This Article REALbasic University: Column 082
OOP University: Part SixLast time we explored the difference between classes and instances: today we continue that lesson with some practical examples.
A New Way of Seeing ThingsThose of you quick on the uptake will realize that our last lesson's explanation of objects makes clear (finally!) one of the most confusing commands offered in REALbasic: the new command. I'm sure you've noticed how sometimes you must "new" something before it will work (such as new date) while other times you don't have to do that. Why do you sometimes have to do it and not others? The explanation is simple. Since catObjectClass is a class, it doesn't exist as an object, so new doesn't apply. However, once you've created an instance of catObjectClass, that instance must be "newed" or it won't work. (The new command reserves memory for the object: the object can't exist without a home!) Thus you'll often find situations where you write code like this:
But here's the kicker: objects dragged to your window have already been "newed" -- that is, REALbasic has gone ahead and taken care of that step automatically. That's why you don't have to "new" a listBox or canvas or other controls you place on windows. (But if you create controls dynamically, you must new them manually.)
The Cat Object DemoThis week I'm including a simple demo project using the cat structure talked about so far. You'll notice a few differences, but it's generally what I've described. All it does right now is display the appropriate cat's picture when you click on the cat's button. ![]() We'll be extending this project in future lessons. For instance, right now the cats are not very object-oriented: they should know how to draw themselves. We'll be doing that in the future. Note that by default the project is set to crash when you run it: you must figure out why and fix it! (It's very easy as I explain exactly what to do within comments. :-) But it should help reinforce today's lesson. If you would like the complete REALbasic project file (including resources), you may download it here.
Instance ReferencesNow the way REALbasic works you might be misled into believing that mayhemInstance (from the previous example) is actually the instance of catObjectClass. In truth, it is not. The instance of the object itself is a structure somewhere in memory. Where is unimportant -- REALbasic handles all the dirty details behind the scenes for you. The actual variable mayhemInstance isn't really an instance, it's a reference to the instance. What does that mean? Well, if you're familiar with traditional programming languages, you've probably dealt with pointers. A pointer is simply a reference to a specific location of memory. Remember, to a computer, all memory is the same -- there's simply a long sequential list of memory addresses. Imagine a huge post office with a few million (or billion) P.O. boxes. A pointer is the equivalent of a P.O. box number. Most programming languages include a pointer data type. REALbasic doesn't seem to, but it really does: any object data type is really just a pointer to that object. That's why objects can be nil -- nil means a nil pointer, a pointer that's pointing at nothing. Why do I bother to explain this? Because it's important. REALbasic's an awesome language, and the way it hides the ugly details of pointers behind an elegant language is wonderful, but when the metaphor breaks down, it causes problems. For example, have you ever tried something like this?
The above code doesn't work, at least not how you're expecting it to work. You're expecting that you can compare two date objects -- but d1 and d2 are not date objects! Remember, they are just references (pointers) to the real objects. Go back to our P.O. box metaphor. Let's say the system puts the date object referred to by d1 into box number 1074 and the date object referred to by d2 into box number 4383. So what the above code is saying is this:
Of course that makes no sense at all! The real way to solve this comparison problem is like this:
This works because we're not comparing instance references, but actual scalar data types (doubles). The problem of confusing instance references with the actual instance of an object is also what causes the following incorrect code:
Again, the programmer is misunderstanding the way objects work in REALbasic. The references object1 and object2 are not objects -- only pointers. By saying object2 = object1 you aren't copying object1.text to object2.text, you're saying that object2 should point the same P.O. box as object1! Let's look closer at this with a practical example. I've created a simple project (you can download it here). It uses a custom class called myClass which contains a single string property called theContents. When the program runs two instances of myClass are instantiated, and the contents of those are drawn within the main window: ![]() Play around with this: when you change the content of a text field on the left and push the corresponding button on the right, that object's contents gets updated. However, once you click the "Copy" button (which executes a simple anObject2 = anObject1, which does not copy the object only the reference to the object), the updated contents of the instances are always the same. ![]() This happens because there's now only one instance! Before we had two references, each pointing to a different P.O. box (instance). After clicking the "Copy" button there are still two references, but they now point to the same P.O. box (instance)! So the two references appear to have the same contents and act the identically.
For the most part within RBU I'll refer to instance reference as instances. It's just easier that way and it makes sense 90% of the time. Just occasionally, though, like when you assume you can copy one object to another, the metaphor doesn't work, so it's very important that you understand the distinction between an instance reference and an instance. Whew! Lots of complicated stuff. I hope your brain's catching all this properly. We're just getting to the good stuff. Next WeekWe add actions to our objects!
NewsThanks to Dong Li for managing the Chinese RBU website. Note that it has moved to a new location, so update your bookmarks! By the way, though I can't read Japanese or Chinese, the sites sure look cool under Mac OS X: instead of gibberish you usually get when visiting a foreign character site, the Japanese and Chinese characters are displayed properly. They look awesome. Check them out!
LettersToday we've got a simple but intriguing question from Judy. She writes:
An intriguing question, Judy! I honestly didn't know the answer, so I asked REAL Software engineer Joe Strout, who's on the REALbasic Developer Editorial Board and frequent contributor, and he had this to say:
So there you have it! About the Column REALbasic University is a weekly instructional column on programming with REALbasic and is brought to you by REALbasic Developer, the magazine for REALbasic programmers. Each week we answer select reader questions, and we're always open to ideas for future columns. Send your questions to . (Keep your questions simple and specific. General queries like "How do I write my own web browser?" will be neglected.) Your question won't be answered immediately, but will be answered in a future column. (If you don't want your correspondence published, just be sure to indicate that when you write. Otherwise it's fair game.) About the Author See the REALbasic University Archives
REALbasic University contents ©2001-2004 by Marc Zeedar and REALbasic Developer. All Rights Reserved.
| |||||||||||||||||||||||||||||||