xHarbour Reference Documentation > Statement Reference xHarbour Developers Network  

METHOD (declaration)

Declares the symbolic name of a method.

Syntax

METHOD <MethodName>[(<params,...>] )] [INLINE <expression>] [SYNC]

Arguments

<MethodName>
This is the symbolic name of a single method to declare. It must begin with a letter or underscore followed by digits, letters or underscores. A symbolic name can contain up to 63 characters.
(<params,...>)
A declaration of formal parameters enclosed in parentheses is required when the method is declared as INLINE. If INLINE is not used, the method can be declared without parameters.
INLINE <expression>
This is the expression which is executed when an INLINE method is invoked. <expression> must be one line of code. The code cannot contain commands but only function and method calls.
SYNC
The SYNC option declares a "synchronized" method. This is only required in multi-threaded applications when a method must be protected against simultaneous access by multiple-threads. Methods declared with the SYNC attribute can be executed only by one thread at a time. Refer to function StartThread() for more information on multiple threads.

Description

When the METHOD statement occurs in the class declaration between CLASS and ENDCLASS, it declares the symbolic name of a method. This name must be sent to an object to invoke the method.

The program code for a declared method must be implemented with a separate METHOD declaration that follows the ENDCLASS statement. If, however, the method code can be programmed in one line, method declaration and implementation can be coded within the class declaration using the INLINE option. INLINE methods are not implemented separately from the class declaration.

When a method is invoked, the object executing the method is accessible in the code with the reserved variable name self. This can be abbreviated with two colons (::).

Info

See also:ACCESS, ASSIGN, CLASS, CLASSMETHOD, DATA, DELEGATE, EXPORTED:, HIDDEN:, INLINE METHOD, MESSAGE, METHOD (implementation), METHOD...OPERATOR, METHOD...VIRTUAL, PROTECTED:, StartThread(), VAR
Category: Class declaration , Declaration , xHarbour extensions
Header:hbclass.ch
Source:vm\classes.c
LIB:xhb.lib
DLL:xhbdll.dll

Examples

METHOD declaration

// The example declares a simple class with a regular and an
// INLINE method. Note that the INLINE method consists of two
// expressions. The first is an assignment and the second is the
// return value of the method. Both expressions are comma separated
// and enclosed in parentheses.

   #include "Hbclass.ch"

   PROCEDURE Main
      LOCAL obj

      CLS
      obj := Test():new( "xharbour" )

      obj:print()
   RETURN


   CLASS Test
      HIDDEN:
      VAR name

      EXPORTED:
      METHOD init( cName ) INLINE ( ::name := cName, self )
      METHOD print
   ENDCLASS

   METHOD print CLASS Test
      ? ::name
   RETURN self

 

SYNC method

// The example implements a Queue class that adds a value to a queue
// with method :put(). Method :get() retrieves the last value added to
// the queue. The queue itself is an array. Both methods are declared with
// the SYNC attribute to protect the queue array from simultaneous access
// by multiple threads.
//
// The Main thread puts values into the queue, while four other threads
// retrieve values from the queue.

   #include "hbclass.ch"

   PROCEDURE Main
      LOCAL i, aT[4], oQueue := Queue():new()
      CLS

      FOR i:=1 TO 4
         aT[i] := StartThread( @Test(), oQueue )
      NEXT

      FOR i:=1 TO 4000                 // Write values
         oQueue:put(i)                 // to queue
      NEXT

      WaitForThreads()                 // Wait until all threads
   RETURN                              // have terminated


   ** This code runs simultaneously in 4 threads
   PROCEDURE Test( oQueue )
      LOCAL i, nRow, nCol

      nRow := GetThreadID() + 10
      nCol := 0
                                       // Read values
      FOR i:=1 TO 1000                 // from queue
         DispOutAt( nRow, nCol+20, oQueue:nCount )

         DispOutAt( nRow, nCol+40, oQueue:get()  )
      NEXT
   RETURN


   ** class with SYNC methods protecting the :aQueue array
   CLASS Queue
      PROTECTED:
      VAR aQueue INIT {}

      EXPORTED:
      VAR nCount INIT 0 READONLY

      METHOD put() SYNC
      METHOD get() SYNC
   ENDCLASS


   METHOD put( xValue )  CLASS Queue
      AAdd( ::aQueue, xValue )
      ::nCount ++
   RETURN self


   METHOD get() CLASS Queue
      LOCAL xReturn

      IF ::nCount > 0
         ::nCount --
         xReturn := ::aQueue[1]

         ADel ( ::aQueue, 1 )
         ASize( ::aQueue, ::nCount )
      ENDIF
   RETURN xReturn

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