# METHOD

Declare a METHOD for a class in the class header

Syntax

  METHOD <MethodName>( [<params,...>] ) [ CONSTRUCTOR ]
  METHOD <MethodName>( [<params,...>] ) INLINE <Code,...>
  METHOD <MethodName>( [<params,...>] ) BLOCK <CodeBlock>
  METHOD <MethodName>( [<params,...>] ) EXTERN <FuncName>([<args,...>])
  METHOD <MethodName>( [<params,...>] ) SETGET
  METHOD <MethodName>( [<params,...>] ) VIRTUAL
  METHOD <MethodName>( [<param>] ) OPERATOR <op>
  METHOD <MethodName>( [<params,...>] ) CLASS <ClassName>

Arguments

  <MethodName> Name of the method to define
  <params,...> Optional parameter list

Description

  Methods are "class functions" which do the work of the class. All methods must be defined in the class header between the CLASS and ENDCLASS commands. If the body of a method is not fully defined here, the full body is written below the ENDCLASS command using this syntax:
  METHOD <MethodName>( [<params,...>] ) CLASS <ClassName>
  Methods can reference the current object with the keyword "Self:" or its shorthand version "::".
  CLAUSES:
  CONSTRUCTOR Defines a special method Class Constructor method, used to create objects. This is usually the New() method. Constructors always return the new object.
  INLINE Fast and easy to code, INLINE lets you define the code for the method immediately within the definition of the Class. Any methods not declared INLINE or BLOCK must be fully defined after the ENDCLASS command. The <Code,...> following INLINE receives a parameter of Self. If you need to receive more parameters, use the BLOCK clause instead.
  BLOCK Use this clause when you want to declare fast 'inline' methods that need parameters. The first parameter to <CodeBlock> must be Self, as in:
  METHOD <MethodName> BLOCK {|Self,<arg1>,<arg2>, ...,<argN>|...}
  EXTERN If an external function does what the method needs, use this clause to make an optimized call to that function directly.
  SETGET For calculated Data. The name of the method can be manipulated like a Data element to Set or Get a value.
  VIRTUAL Methods that do nothing. Useful for Base classes where the child class will define the method's behavior, or when you are first creating and testing a Class.
  OPERATOR Operator Overloading for classes. See example Tests/TestOp.prg for details.
  CLASS <ClassName> Use this syntax only for defining a full method after the ENDCLASS command.

Examples

          CLASS TWindow
             DATA   hWnd, nOldProc
             METHOD New( ) CONSTRUCTOR
             METHOD Capture() INLINE  SetCapture( ::hWnd )
             METHOD End() BLOCK  { | Self, lEnd | If( lEnd := ::lValid(),;
                                     ::PostMsg( WM_CLOSE ),), lEnd }
             METHOD EraseBkGnd( hDC )
             METHOD cTitle( cNewTitle ) SETGET
             METHOD Close() VIRTUAL
          ENDCLASS

          METHOD New( ) CLASS TWindow
             local nVar, cStr
             ...  ...
             ...  ...
          RETURN Self
    Tests
          TestOp.prg

Status

  Ready

Compliance

  METHOD is a Harbour extension.

Platforms

  All

See Also

- HBClass()
- Object Oriented Programming
- DATA
- CLASS