| |||||||||||||||||||||||||||||||||||||||
|
| |||||||||||||||||||||||||||||||||||||||
Print This Article REALbasic University: Column 081
OOP University: Part FiveOur last lesson wasn't too long as we shook hands with objects, but today we get more complicated. (I also should apologize for the lack of updates: I was at Macworld Expo and have gotten behind. I will try to publish two columns this week to catch up.)
InstancesWhat we've learned so far is about categories and characteristics. In OOP terms, those are known as classes and properties. A class is just a category of object. A property is just a characteristic of an object. Simple! But then we throw in an OOP concept that really confuses things. Instances. What the heck is an instance? For a moment, let's go back to our scientist classifying objects. He creates (in OOP terms) classes of objects. He creates a class that describe a cat object. But it is not a cat. It is nothing. It's not even an object. It's only a definition, or description, of a cat object. This is subtle but so important, let me repeat it: Why is that so important? Because that is the difference between a class and an instance. A class describes what an object is like. An instance is the object. A instance is Mischief or Mayhem. Mischief and Mayhem are instances of catObjectClass. An instance is an actual cat object, not a description of a cat object. Got that? It's crucial you understand that. With no formal training it took me years to figure out the distinction. The differences between classes and instances is one of the most basic principles of object-oriented programming. Here's another critical way the two differ. Since a class is just a description, a list of characteristics, its properties cannot actually have values. That basically destroys the classification metaphor I've been using all along and is one of the biggest challenges in understanding OOP. For instance, in the chart I included earlier, I put in "alive = true" under the animateObjectClass. While that is how a scientist would categorize objects, that's not how OOP works. Technically you couldn't do that in object-oriented programming. You could define the "alive" property (specify the kind of data that property will hold, such as boolean, string, or integer), but not the value of that property. Remember, a class is just a definition of the properties of an object. Those properties have no value because there is no object yet! Once you've created an instance of an object, then you can begin assigning values to the instance. In other words, a catObjectClass is nothing: but a mischiefInstance is a real cat, Mischief. mischiefInstance has the properties defined in catObjectClass, but initially no values. After you create the instance, you programmatically (or via the Properties window in REALbasic) set the value of those properties. Note that reverse of this concept is also true: you cannot add, change, or delete properties of an instance. Here's a table showing the key characteristics of a class compared to an instance:
Here's a good way to remember this: just as a class is a definition, a property is a definition. Just as an instance is a tangible incarnation of an object, so is a property's value.
Back to REALbasicSo far we've been talking about objects in an abstract fashion. These basic object concepts are the same regardless of language. But since you're familiar with REALbasic and you've probably been using many of these features without understanding them properly, let's take a look at what this looks like in REALbasic. To put this in REALbasic programming terms, a property's definition is it's dim statement:
If you've used REALbasic at all, it's obvious this doesn't set the value of alive, but just defines it as a variable of type boolean. An instance can't define a property, but it can define (set) the property's value:
Here's the definition of catObjectClass (a subset of animateObjectClass):
To create an instance of catObjectClass, we basically create a variable of that object's data type. Like this:
Where do you do this? Ah, that depends on what you are doing with the object. You could define it as a property of a window or module (you wouldn't need the "dim" command in that case), or it could be within a pushButton, method, or any other code block (though it you do it there, the object instance is only accessible within that code block). There is also another way to create an instance: you can drag catObjectClass from your project window to a window in your project (such as Window1). This is exactly like dragging a listBox or other predefined control to your window! When you do this, REALbasic will create an instance of the object and place it on the window. You're free to select it and modify its properties. More on this next time!
Next WeekWe explore these concepts further with an example project.
LettersThis week Ken writes with a question about Tab Panels.
The short answer: no. The only way to add new tabs is within the IDE. However, you can change the name of tabs programatically, so you could create extra tabs in advance and change their names appropriately while running. This would only be practical if the additional tabs you needed were a fixed quantity. The key disadvantage of this method is that the "blank" (unused) tabs are visible. Theoretically you could work around this if you only needed a few tab amounts by creating in advance several tabPanels, each with a different number of tabs, and only make one of them visible or within the window's boundaries. But that's rather convoluted. A better solution might be to use a pagePanel (pagePanels were introduced with REALbasic 4.5). A pagePanel is similar to a tabPanel, except that the panel itself is invisible. This allows you to implement whatever graphical look and interface you'd like for the panel. You could have a series of icons or text labels for the user to click on, arrows or a scroll bar, whatever works. It's up to you to programmatically control which tab is currently displayed. While a pagePanel also does not allow you to add panels programatically, since the panels are invisible, you could create a pagePanel with the maximum number of panels you'd need (say 20 or 50) and just not use the extra ones. Remember, you control the interface and display of the panels, so it would be easy to not let the user advance past panel 10 if that was the final panel. I did this with a simple demo program: ![]() This demo lets you dynamically set the number of panels (up to a max of 22) and then scroll through them. After you change the number of panels, the scrollbar updates to reflect the new maximum. Of course in your own program you'd implement a much better visual interface! 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.
| |||||||||||||||||||||||||||||||||||||||