home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Demos / Codeworks 0.94b3 / Codeworks® WWW Demo Doc. / Scripting Manual.doc / Scripting Manual.doc.rsrc / TEXT_138.txt < prev    next >
Encoding:
Text File  |  1995-05-01  |  6.5 KB  |  93 lines

  1. Objects - Instance Variables
  2.  
  3.     When you create an object, that object can have instance variables.  These are variables that belong to the object.  As long as the object isn‚Äôt destroyed, its instance variables continue to exist and hold the last value they were set to.  In this way, instance variables are much like the workspace variables discussed above.  In fact, workspace variables are just instance variables of the workspace object!
  4.  
  5.  
  6. Creating an Object and its Instance Variables
  7.     Create a new class called cash-register and add three fields to it named amount, sub-total, and daily-total.  To do this:
  8. 1.    Tap on New Class‚Ķ from the Utilities menu.  The class creator window appears.
  9. 2.    Enter the name of the class to be cash-register, and select the top-most window style icon.
  10. 3.    Tap Create & Close.  The class creator window will close and the new cash-register object will open.
  11. 4.    For each field:
  12. a.    Caret in an empty place in the grid.  A pop-up list of field types appears.  Choose an empty place in the right column.
  13. b.    Tap Number in the pop-up menu.  A number field is added to the cash-register object.
  14. c.    Circle on the new field to rename it.
  15. d.    Edit the name to one of amount, sub-total, or daily-total, then tap OK.
  16.  
  17. 5.    If you want, you can add labels beside the fields and edit the labels to name the fields.
  18.  
  19.     You now have created an object, named cash-register.  This object has three instance variables (or properties) named amount, sub-total and daily-total.  Cash-register also has a form-like view that displays these three properties in numeric write-in fields.  If you toggle the mode of the window to user-mode (the aligner lines disappear), you can edit the values of the variables in the cash-register windows.  If you do this, toggle the mode back to author-mode (the aligner lines appear again) before proceeding.
  20.  
  21.  
  22. Accessing the Instance Variables
  23.     When you write scripts for an object, its instance variables can be accessed and set by name.  You will write a script for cash-register that clears all three variables to zero:
  24.  
  25. 1.    Add a button to cash-register.  Circle the button and rename it ‚ÄòClear‚Äô.
  26. 2.    Tap on the button to edit its script.
  27. 3.    Replace the entire script with:
  28.     
  29.         $ clear-daily.
  30.         clear-daily := ask user "Clear daily-total too?".
  31.         amount := 0.
  32.         sub-total := 0.
  33.         if clear-daily then [ daily-total := 0 ].
  34.  
  35. 4.    Tap Save.  Make sure there were no errors.  A common error is to get the message ‚ÄúUnknown local or property‚Äù.  This generally means that you either misspelt the name of a property in the script, or when you named the property.  Correct the misspelling in the script or of the property and save again.
  36.  
  37.     When you add a button, you are adding a script property to an object.  Tapping the button (in user-mode) causes the script to run.  Toggle cash-register into user-mode and try it.
  38.  
  39.     In the rest of the manual, you will be asked to add buttons to objects with various scripts. To save space, the detailed instructions will not be give.  Just follow the procedure above.
  40.  
  41.  
  42.  
  43. ¬ª The Ask Message
  44.     The script above uses a new message: ask sent to user.  User is an object that represents the real user to your scripts.  The ask message puts up a note asking a question and offers two choices: Yes and No.  If the user taps Yes, the result of the ask message is true, otherwise the result is false.  Notice how the script stores the result of the ask message in a local variable and then use that result later.
  45.  
  46.     It is better practice to offer the user the option of canceling an operation as destructive as clear.  Fortunately, the ask message has just such an option.  Replace the first line in the script above with these two lines:
  47.     
  48.         clear-daily := ask user "Clear daily-total too?" with-cancel.
  49.         if (clear-daily is ???) then [ return ].
  50.  
  51.     The optional argument with-cancel directs the ask message to include a Cancel button in the note.  If the user taps Cancel instead of Yes or No, then the result of the ask message is ??? (unknown).  The if statement tests for this case and executes a return if so, stopping all further processing of the script.
  52.  
  53.  
  54. Accessing Scripts from Elsewhere
  55.     Once you have defined the Clear button above, the object cash-register will now understand the message clear.  In a workspace you can execute:
  56.  
  57.         clear cash-register.
  58.  
  59.     This will act exactly as if you had tapped the Clear button.  In fact, when you tap the Clear button, this is the expression the system evaluates.
  60.  
  61. ¬ª    Remember that the order of the message and the receiver can be reversed.  If you find it reads better, you can write the expression above as:
  62.  
  63.         cash-register clear.
  64.  
  65.  
  66.  
  67. Accessing Instance Variable from Elsewhere
  68.     In a script of an object (like the script of the Clear button), expressions can refer to that object‚Äôs instance variables simply by name.  However, if you want to access the instance variables from a script that doesn‚Äôt belong to the object (from a workspace, for example), then you must use a different syntax.
  69.  
  70.     To refer to the instance variable of another object, you use dot-notation.  You write the name of the object, a period, and the name of the variable.  For example, in the workspace you can execute:
  71.  
  72.         cash-register.sub-total := 22.50.
  73.         cash-register.total := cash-register.total + cash-register.sub-total.
  74.  
  75. !!    There are no spaces around a period used in dot-notation.  If you accidentally put a space in, then the system will confuse the period for the period at the end of an expression.
  76.  
  77.  
  78. ¬ª    Notice that dot-notation is exactly the same as the short-hand notation for messages that have no arguments.  In fact, instance variables and messages with no arguments can be each be written in the same three different ways.  Across each line, the three expression are equivalent and do the same thing:
  79.  
  80.     Dot-Notation                   Message After                Message Before
  81.     cash-register.total           cash-register total      total cash-register
  82.     cash-register.clear          cash-register clear     clear cash-register
  83.     
  84.     Having three different ways of writing the same thing may be a bit confusing at first, but it allows you to choose the best way of writing an expression.
  85.  
  86.  
  87.     Sometimes it is convenient to set a local variable to an object.  When you do this, the variable refers  to the object.  This is the same as saying the value of the variable is the object, but it makes it clear that any use of the variable will really use the object.  For example, the above script could have been written as:
  88.  
  89.      $ cr.
  90.         cr := cash-register.
  91.         cr.sub-total := 22.50.
  92.         cr.total := cr.total + cr.sub-total.
  93.