REALbasic University Resources:

RBU: Glossary Defines common REALbasic programming terms
  Archives Previously published columns
Translations: Dutch Courtesy of Floris van Sandwijk
  Japanese Courtesy of Kazuo Ishizuka
  Chinese Courtesy of Dong Li
  RBU Translation Guide Information on Translating RBU into other languages
Books: Matt's Book (2nd Edition!) Ideal for experienced programmers
  Erick's Book Best for beginning programmers
Websites: Mother Ship The publisher of REALbasic
  RB Webring Links to hundreds of REALbasic websites
  RESExcellence Another REALbasic programming column
  REALbasic Developer Magazine The premiere source for REALbasic instruction.

REALbasic University is Sponsored by

Make your Mac do what YOU want it to. Create games, utilities, cool Mac OS X tricks. Download REALbasic now and create your own software.


Print This Article

REALbasic University: Column 081

OOP University: Part Five

Our 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.)

Instances

What 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:

A class is nothing but a description.

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:

Class Can add or change properties
Cannot set property values
Cannot delete properties
Instance Can set property values
Cannot add or change properties
Cannot delete properties

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 REALbasic

So 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:

  
alive as boolean

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:

  
alive = true

Here's the definition of catObjectClass (a subset of animateObjectClass):

  
Class catObjectClass
dim legs as integer
dim mammal as boolean
dim personality(0) as string
dim species as string
dim theColor as string
dim thePicture as picture
end Class

To create an instance of catObjectClass, we basically create a variable of that object's data type. Like this:

  
dim mischiefInstance as catObjectClass

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 Week

We explore these concepts further with an example project.

Letters

This week Ken writes with a question about Tab Panels.

Is it possible to add additional tabs to an existing tab panel during runtime... I'm working on an application that reads a stream via TCP/IP. When it's parsed into the separate segments, I want to create a tab for each one and then be able to place the information found in each segment on its respective tab panel.

Thanks
Ken

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
is an author, philosopher, graphic designer, photographer, film director, soccer fanatic, and programmer (among other things). He writes for MacOpinion, runs his own software company, Stone Table Software, which sells the revolutionary Z-Write word processor, and is Publisher and Editor of REALbasic Developer. He lives in Northern California with his cats, Mischief and Mayhem, and is rapidly running out of free time.

See the REALbasic University Archives


REALbasic University contents ©2001-2004 by Marc Zeedar and REALbasic Developer. All Rights Reserved.

Email This Article - Comment On This Article

.

Reader Specials

Server Racks Online:
Apple Xserve CompatibleServer Racks and Universal Network Racks
42U KVM Switch Solutions:
High-End Mac and Multi-Platform KVM Matrix switching solutions!
Digital Camera Online:
Great prices on Digital Cameras and accessories!
KVM Switches Online:
Great prices on Mac KVM Switches from the leading manufacturers!
LCD Monitors Online:
Great prices on LCD Monitors from the leading manufacturers!
LCD Projectors Online:
Shop online for LCD Projectors from the leading manufacturers!
USB 2.0 Online:
Great prices on USB 2.0 products from the leading manufacturers

Serious Business Software:
Accounting, Sales, Inventory, CRM, Shipping, Payroll & more!

KVM Switch solutions for MACs:
DAXTEN is a KVM switch, KVM extender and monitor splitter specialist for PC, SUN and MAC applications from name brand manufacturers - offices worldwide.

The "Think Different Store: The iPod Accessories Store - iPod cases, iPod mini, iPod photo, speakers, itrip, inMotion, Soundstage and all other iPod accessories

Earn Cash with the ThinkDifferent Store Affiliates Program

Need A Web Site?
Applelinks Web Hosting Starting at 19.95 a Month

iTunes_RGB_9mm

.

iTunes_RGB_9mm

Cool Mac Gear


iPod 1G-2G
iPod 3G
iPod 4G
iPod Mini
PowerBook-iBook
Keyboard Skins
Garageband