Syntax
Declares a function that belongs to a TYPE. Inside it, SELF is the record the method was called on.
METHOD <method>([<parameter>, …])
SELF.<field>
EXITMETHOD [<value>]
RETURN [<value>]
ENDMETHOD
<record>.<method>([<argument>, …])Also written : ENDMETHOD, EXITMETHOD, SELF
Parts
| <method> | The method name. Case does not matter. New and Delete are special: see NEW and DELETE. |
|---|---|
| <parameter> | A parameter, written as for a FUNCTION: typed, with a default value, OPTIONAL, REF or variadic. |
| <field> | A field of the record, or another method: SELF.Reset(). |
| <value> | The value given back by the method. Optional. |
| <record> | Any record: a variable, an array cell (counters(2)), the current list element (counters()). |
| <argument> | A value passed to the matching parameter. |
Good to know
- A method is declared inside its TYPE, among the fields. The parentheses are required, even empty:
METHOD Reset(). - Writing
SELF.valuechanges the record the method was called on, also when it is an array cell or a list element. EXITMETHOD <value>andRETURN <value>leave the method with a value.ENDMETHODtakes no value, unlikeENDFUNCTION.- A method has no return type:
METHOD Half() AS integeris refused ("Invalid field in BASIC type"). Convert the value yourself before returning it. - Refused at compile time: a METHOD outside a TYPE ("METHOD requires a containing TYPE"), a METHOD inside a METHOD, two methods with the same name in one TYPE, a value after
ENDMETHOD, andEND METHODin two words. - Do not name a method
Length,Find,Sort,InsertorRemove. These names belong to the array members, and the call stops the program. - A copy of a record has the same methods. In a program with
DO … LOOP, a program variable used by a method must be declaredGLOBAL, as for any FUNCTION.
Example
TYPE Counter value AS integer METHOD Add(amount AS integer) SELF.value = SELF.value + amount EXITMETHOD SELF.value ENDMETHOD METHOD Reset() SELF.value = 0 ENDMETHODENDTYPEc AS CounterConsole(Str(c.Add(5))) ' 5Console(Str(c.Add(3))) ' 8c.Reset()Console(Str(c.value)) ' 0In the manual : Records and methods