xHarbour Reference Documentation > Statement Reference xHarbour Developers Network  

MESSAGE

Declares a message name for a method.

Syntax

MESSAGE <MessageName> METHOD <MethodName>
MESSAGE <MessageName> IN     <SuperClass>
MESSAGE <MessageName> IS     <MethodName> IN <SuperClass>

MESSAGE <MessageName> TO     <ContainedObject>   
MESSAGE <MessageName> IS     <MethodName> TO <ContainedObject>

Arguments

<MessageName>
This is the symbolic name of a message to declare for a class.
METHOD <MethodName>
This is the symbolic name of the method to execute when an object receives <MessageName> as a message.
IN <SuperClass>
Optionally, the name of the super class can be specified to which the message should be sent. This requires the class to inherit one or more other classes.
IS <MethodName>
When a message should be directed to a super class, the method to invoke in the super class can be specified with <MethodName>.
TO <ContainedObject>
This is the symbolic name of an instance variable holding the object that receives the message.

Description

The MESSAGE statement can only be used within the class declaration between CLASS and ENDCLASS. It declares a message that invokes a method of a different name in the declared class or its super class.

MESSAGE is used to resolve ambiguities when a class inherits from one or more super classes which have the same method names. The statement can also be used to invoke a method via an alternative name.

Info

See also:ACCESS, ASSIGN, CLASS, DATA, DELEGATE, METHOD (declaration), VAR
Category: Class declaration , Declaration , xHarbour extensions
Header:hbclass.ch
Source:vm\classes.c
LIB:xhb.lib
DLL:xhbdll.dll

Example

// In the example a Subclass uses the method implementation
// of a Super class but redefines the message to send to
// an object. Message :print() invokes method :show().

   #include "Hbclass.ch"

   PROCEDURE Main
      LOCAL obj := TextFile():new( "Text.prg" )

      obj:open()
      obj:print()
      obj:close()
   RETURN


   CLASS Text
      EXPORTED:
      DATA string

      METHOD init(c)  INLINE ( ::string := c, self )
      METHOD show     INLINE QOut( ::string )
   ENDCLASS



   CLASS TextFile FROM Text
      EXPORTED:
      DATA fileName

      METHOD init
      METHOD open
      METHOD close

      MESSAGE print IS show IN Text
   ENDCLASS

   METHOD init( cFile, cText ) CLASS TextFile
      ::fileName := cFile

      IF Valtype( cText ) == "C"
         ::text:init( cText )
      ELSE
         ::text:init( Memoread(::fileName ) )
      ENDIF
   RETURN self

   METHOD open CLASS TextFile
      SET ALTERNATE ON
      SET ALTERNATE TO (::fileName)
   RETURN

   METHOD close CLASS TextFile
      SET ALTERNATE TO
      SET ALTERNATE OFF
   RETURN

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