Home
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Lego
Through
the Origin

Squares (5)

(previous pagetutorialsnext page)

A little more about Object-Oriented Programming.

Here is what we really have:

On the right you see the objects:  there is a stack Square, with one card.  On the screen you can't really see that the card sits inside the mainstack.  In this diagram I have coloured the card light grey to distinguish it from the stack.  The card has two fields and one button.

The green lines show the connections between the objects and their scripts.  There are two scripts, and each has only one handler.

When you start up the program, the system sends a start-up message.

That start-kick goes to the only object that actually has a piece of code that takes care of it:  the mainstack itself.  The two lines "put empty ..." will be executed.  The fields will be cleared.  And then the program sits there waiting for you.

Then you type something in the number field.  Nothing happens.  Then you let the mouse go up:  nothing happens unless the mouse goes up inside the Compute button.

Why? because the fields and the card do not have handlers to deal with a MouseUp event.

Important points:

A variant

Now, let's make a little variant.
First, go back to the Square.rev file.  That's the file you can modify, not the standalone application file.

Select the select tool, not the browse tool.
Select Object—>Stack Script and change it to this:

on StartUp

ClearFields

end StartUp

 

on ClearFields

put empty into field "Number"

put empty into field "Square"

end ClearFields

Don't forget to press the Apply button!

You have just written your first subroutine.  In other programming languages ClearFields would be called a procedure, a subroutine, a function or just a routine.  In object-oriented programming and specifically in Transcript it is called a handler.

ClearFields is not an event.  It's just a name we invented.  We wrote a handler for ClearFields, which is an action that we invented ourselves.

Now select Object—>Card Script and type there:

on MouseUp

ClearFields

end MouseUp

Press the apply button.

Now we have two handlers for the event MouseUp.

The object diagram now looks like this:

Now run the program (by choosing the hand tool, not the arrow).

Click and let the mouse go up.  What happens?

It depends on where you let the mouse go up.

If it is in the card, i.e. not on a field or a button, then it's the card that will "catch" the MouseUp event.  It will try to do ClearFields.  There is no Clearfields handler in the script of the card.  The card will then call for help, and it will hand the call for ClearFields to the next person "up the ladder":  the master just above the card, which is the stack.  The stack will look in its script and find a ClearFields routine.  It will execute it and the fields will get cleared.

If on the other hand you let the mouse go up inside the button, then it's the button that will catch the event, it will look in its script for a MouseUp handler and it will put the square of the number into the square field.

(previous pagetutorialsnext page)





next planned revision: 2006-11-30