Publishers of technology books, eBooks, and videos for creative people

Home > Articles > Web Design & Development > PHP/MySQL/Scripting

  • Print
  • + Share This
This chapter is from the book

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.37Figure 4.37 The parts of a method.

To create a method:

  1. With the Code Editor open, choose Edit > New Method.

  2. In the New Method dialog, enter a name, parameters (optional), and a return value for the method.

  3. In the Code Editor, click the disclosure triangle to show the user-defined methods.

  4. 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.38Figure 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:

  1. Open the Code Editor for the window in which the method resides.

  2. Click the disclosure triangle next to Methods in the left pane to see the method's name.

  3. Click the icon next to the method's name to select it.

  4. 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.

  • + Share This
  • 🔖 Save To Your Account