- Creating Behavior Without Programming
- Using the Code Editor to Write REALbasic Code
- Getting Help with the REALbasic Language
- Mastering Dim and Assignment Statements
- Making Tests and Comparisons
- Writing Code that Branches
- Writing Code that Repeats
- Writing Your Own Methods
- Extending the HTML Editor
- Creating the Indent Menu Item
- Removing Existing Indentation
- Inserting Indentation Before Tags
- Handling the Indent Level
- Extending the Project
Writing Your Own Methods
The code you write for a REALbasic application resides in its methods. Event handlers and menu handlers are methods, and you can create your own custom methods, as you did in Chapter 3. The code is packaged in these discrete chunks because this type of programming is event-driven. The code in an event handler springs into action when the events that the handler handles occurs; a menu handler's code performs its magic when the user selects a menu item; and the code in a custom method is executed when the method is invoked by some other piece of code.
The parts of a method (Figure 4.37) are:
The method's name. The name is used to refer to or call the method in your code.
The parameters (optional). If you pass values to your method, the method needs a way to refer to those values. A method's parameters are the names by which it refers to these values.
The return value (optional). The return value is used to return a value to the calling code.
The method body. The method body is the code that the method executes.
Figure 4.37 The parts of a method.
To create a method:
With the Code Editor open, choose Edit > New Method.
In the New Method dialog, enter a name, parameters (optional), and a return value for the method.
In the Code Editor, click the disclosure triangle to show the user-defined methods.
Click the name of the method you just created, and enter its code in the editor pane of the Code Editor (Figure 4.38).
Figure 4.38 Creating a method.
To use a method in your code:
If your method has no return value, use it as you would a built-in REALbasic command:
indent(HtmlField.text,lt)
Note that the parameters are enclosed in parentheses.
or
If your method has a return value, use it as a value, where a value of that type would be appropriate:
HtmlField.text = indent(HtmlField.text,lt)
To pass values to a method:
Put the values in parentheses method name.
The values you supply may be values or variables. They must the same order as the parameters defined for the method, data types, and they must be by commas if you use more
To return a value from a method:
If you have defined a return value for a method, use the call to the method just as you would use a value of the data type of the return value.
To delete a method:
Open the Code Editor for the window in which the method resides.
Click the disclosure triangle next to Methods in the left pane to see the method's name.
Click the icon next to the method's name to select it.
Choose Edit > Delete, or press Command-Delete.
Tip
A method that does not return a value is sometimes called a procedure. You can use it in your code as though it were a built-in REALbasic command. A method that returns a value is called a function; you can use it in your code as though it were a variable of the appropriate data type. Think of functions as being nouns and procedures as being verbs.