xHarbour Reference Documentation > Statement Reference xHarbour Developers Network  

METHOD (implementation)

Declares the implementation code of a method.

Syntax

METHOD <MethodName>[( <params,...> )] CLASS <ClassName>

  <...program code...>

RETURN <retVal>

Arguments

<MethodName>
This is the symbolic name of the method to implement. It must begin with a letter or underscore followed by digits, letters or underscores. The symbolic name can contain up to 63 characters.
(<params,...>)
If a method accepts parameters, they must be declared as a comma separated list within parameters.
CLASS <ClassName>
This is the symbolic name of the class where the method is declared.
RETURN <retVal>
The RETURN statement terminates a method and branches control back to the calling routine, returning the value <retVal> to it.

Description

When the METHOD statement occurs outside the class declaration after ENDCLASS, it declares the implementation part of a method. A method is implemented like a FUNCTION, including formal parameter list, if required.

There is one important difference to functions: a special variable self exists in all methods. self is the object executing the method. Via self instance variables of an object can be accessed and other methods of the class can be invoked.

The name of the class where the method is declared must be specified with the method implementation. Without the class name, it would not be possible to implement multiple classes having the same method names in one PRG module.

When a method does not require a particular return value, it is quite common to return the self object. This way, method calls can be "chained" in one line of code.

Info

See also:CLASS, FUNCTION, INLINE METHOD, METHOD (declaration), PROCEDURE
Category: Class declaration , Declaration , xHarbour extensions
Header:hbclass.ch
Source:vm\classes.c
LIB:xhb.lib
DLL:xhbdll.dll

Example

// The example implements a class with nine methods that can be
// used for database access which is independent of the current
// workarea.

   #include "HbClass.ch"

   PROCEDURE Main
      LOCAL oWorkArea
                            // method "chaining"
      oWorkArea := WorkArea():new( "Customer.dbf" ):open()

      ? Alias()             // result: Customer

      SELECT 0
      ? Alias()             // result: empty string ("")

                            // method "chaining"
      oWorkArea:goBottom():skip(-5)

      DO WHILE .NOT. oWorkArea:eof()
         ? Customer->LastName
         oWorkArea:skip()
      ENDDO

      oWorkArea:close()
      ? Alias()             // result: empty string ("")

      oWorkArea:open()
      ? Alias()             // result: Customer

      oWorkArea:close()
   RETURN

   CLASS WorkArea
      PROTECTED:
      VAR fileName

      EXPORTED:
      VAR area  READONLY

      METHOD init
      METHOD open
      METHOD close
      METHOD bof
      METHOD eof
      METHOD found
      METHOD skip
      METHOD goTop
      METHOD goBottom
   ENDCLASS

   METHOD init( cFileName ) CLASS WorkArea
      ::fileName := cFileName
      ::area     := 0
   RETURN self

   METHOD open() CLASS WorkArea
      IF ::area == 0
         USE ( ::fileName ) NEW SHARED
         ::area := Select()
      ELSE
         DbSelectArea( ::area )
      ENDIF
   RETURN self

   METHOD close() CLASS WorkArea
      IF ::area <> 0
         (::area)->(DbCloseArea())
         ::area := 0
      ENDIF
   RETURN self

   METHOD bof CLASS WorkArea
   RETURN IIf( ::area == 0, .T., (::area)->(Bof()) )

   METHOD eof CLASS WorkArea
   RETURN IIf( ::area == 0, .T., (::area)->(Eof()) )

   METHOD found CLASS WorkArea
   RETURN IIf( ::area == 0, .F., (::area)->(Found()) )

   METHOD skip( nSkip ) CLASS WorkArea
      IF PCount() == 0
         nSkip := 1
      ENDIF

      IF ::area > 0
        (::area)->(DbSkip(nSkip))
      ENDIF
   RETURN self

   METHOD goTop CLASS WorkArea
      IF ::area > 0
        (::area)->(DbGotop())
      ENDIF
   RETURN self

   METHOD goBottom CLASS WorkArea
      IF ::area > 0
        (::area)->(DbGoBottom())
      ENDIF
   RETURN self

Copyright © 2006-2007 xHarbour.com Inc. All rights reserved.
http://www.xHarbour.com
Created by docmaker.exe