STATEMENTS

METHOD

Declare and implement a class method

Syntax

Signature
CREATE CLASS idClass
   METHOD idMethod( [idParam [, ...]] )
ENDCLASS

METHOD idMethod( [idParam [, ...]] ) [AS resultType] CLASS idClass
   statements...
   [RETURN [exp]]

Arguments

idMethod names the method. idClass identifies its class. The implementation may declare types for parameters and the result.

Description

Call a method with oObject:idMethod( ... ). Self is the current object. Within a method, :: names a member of Self. Use RETURN exp to give the caller a value.

Example

METHOD.prg
CREATE CLASS Ledger
   DATA balance INIT 100
   METHOD Debit( nAmount )
ENDCLASS

METHOD Debit( nAmount ) CLASS Ledger

   IF nAmount <= 0 .OR. nAmount > ::balance
      RETURN .F.
   ENDIF
   ::balance -= nAmount

   RETURN .T.

PROCEDURE Main()

   LOCAL oAccount := Ledger()
   LOCAL nAmount

   FOR EACH nAmount IN { 70, 45, 30 }
      QOut( "Debit", nAmount, "accepted", oAccount:Debit( nAmount ) )
   NEXT
   QOut( "Remaining balance", oAccount:balance )

   RETURN

Output

See also