Calling Procedure
by Gerald Lightsey

TECHNICALLY a procedure/function, that belongs to an object, (such as a form), is called a METHOD of that object. A procedure that is outside the object is just a procedure, as always.

Lets talk about a METHOD of the form. If you have a pushbutton named pbQuit and you want to create a method that responds to the button when you click it, call the Form Designer, select the pushbutton and in the Property Inspector, click the Events tab, scroll to the event named `onClick` and click the wrench icon on the right hand side. That creates the skeleton of a method in the Source Editor named `PBQUIT_onClick` in which you can write your performance code. Importantly, at the same time the following reference is automatically written into the constructor code for pushbutton pbQuit.

| `onClick = class::PBQUIT_ONCLICK`
| ``

This is the code that tells the pbQuit control object where to find the function, ( same as procedure in the Windows versions of dBASE), that should be performed when the object is clicked. The`::` symbol is called a resolution operator and in this case tells the object that the function is in the same class that defines the object. If the class is a form, then the automatically generated FORM reference `form.<objectName>` goes to the same place.

Now, how to handle a method that will respond to more than one control object? A classic example is a set of three related radio buttons. The operative event is `onChange`. Every time you click one of the buttons its `onChange` event fires but so does the `onChange` event of the previously selected radio button. So what you would like to have is one event handler, (method), where you can code what you want to occur when any one of them is clicked. With a form open in design mode and the radio buttons already named, click on the Method menu item and then click on New Method. The Source editor will create a skeleton method named function `Method`. Change the function’s name to something that means something like function `radiobutton_Change` if the radio buttons are changing what you define as a mode. Write your code to catch only the radio button that is newly selected and what you want to happen.

| `if this.value // only the newly selected button’s value is true` `do case` `case this.name == "RADIOBUTTON1"` `// action code` `case this.name == "``RADIOBUTTON` `2"` `// action code` `case this.name == "``RADIOBUTTON` `3"` `// action code` `endcase` `endif` `return`
| ``

`THIS` is an automatically generated reference possessed by every object that identifies itself when making a method call, (similar to `Sender` used by several other languages).

After setting up the method as described above, and while the cursor is still within the method in the Source Editor, click on the Method menu item and then click on Link Event. A dialog box opens in which you can select the object and the object event that will call the method. In this example select the radiobutton1 object and its `onChange` event and click OK. Repeat for `radiobutton2.onChange()` and `r` `adiobutton` `3.onChange()`. This dialog writes the appropriate calling code into EACH control object constructor code to call your common method just as described earlier for a single pushbutton object calling a single `onClick()` method.

Also, you can write a procedure, (that is not a method of the class) within the same form. Here is an example you can create and run. Open a new form in the Form designer and place one pushbutton on it. In the Source Editor, scroll all the way to the bottom, or click on the bottom
icon in the left treeview pane. Below the `endclass` statement enter the following function:

| `Function writeTest` `? "This is a test"` `return`
| ``

Select the pushbutton and select the wrench icon for the `onClick` event in the Property Inspector. Complete the auto-generated skeleton so it reads…

| `Function PUSHBUTTON1_onClick` `writeTest()` `return`
| ``

The parenthesis`()` following `writeTest` is the procedure/function CALL operator. Now when you run the form and click the button: “This is a test” will be written to the dBASE Command Window Results Pane. This is a contrived example because it obviously is more work to set up this way than to just enter`? "This is a test"` directly in the `PUSHBUTTON1_onClick()` event handler, (method).

Finally, if you have a procedure/function that you wanted to call from another file such as`.PRG`,`.CC`,`.WFM` you simply have to create a
reference to it. For example, in the Form’s `onOpen` event you could enter…

| `set procedure to StringBox.cc additive`
| ``

Thereafter, anywhere you need to call a function named `AllTrim()` from `StringBox.cc` your code would look like:

| `aString = " Some Text "` `aTrimmedString = AllTrim( aString )` `? aTrimmedString // displays "Some Text"`
| ``

Note that the `Additive` qualifier in the `Set Procedure` command that was introduced in Windows versions does just what it says without clearing any other procedures already referenced. Failure to use the `Additive` qualifier is one of the most frequent reasons for code failure in the Windows versions because it destroys all previously initiated procedure links.

Hope it helps.