METHOD

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 <NAME>([<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

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

      METHOD New( ) CLASS TWindow
         local nVar, cStr
         ... <code> ...
         ... <code> ...
      RETURN Self

Tests

      TestOp.prg

Compliance

Harbour

Platforms

All

Seealso

HBClass(), Object Oriented Programming, DATA, CLASS

Advertisements

3 responses to “METHOD

  1. Pingback: Harbour Statements | Viva Clipper !

  2. Pingback: Harbour Commands | Viva Clipper !

  3. Pingback: Harbour RG Summary | Viva Clipper !

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s