xHarbour Reference Documentation > Statement Reference xHarbour Developers Network  

DELEGATE

Declares a message to be directed to a contained object.

Syntax

DELEGATE <Message> TO <ContainedObject>

Arguments

<Message>
This is the symbolic name of the message to delegate. It must begin with a letter or underscore followed by digits, letters or underscores. A symbolic name can contain up to 63 characters.
<ContainedObject>
This is the symbolic name of an instance variable holding the object that receives the message.

Description

The DELEGATE statement can only be used within the class declaration between CLASS and ENDCLASS. It declares a message that is automatically delegated to the object held in the instance variable <ContainedObject>. The message must exist in the class of <ContainedObject>, otherwise a runtime error is raised.

Message delegation is commonly used when the relationship between objects is based on containment rather than inheritance.

Info

See also:ACCESS, CLASS, :
Category: Class declaration , Declaration , xHarbour extensions
Header:hbclass.ch
Source:vm\classes.c
LIB:xhb.lib
DLL:xhbdll.dll

Example

// The example outlines a typical usage scenario for message delegation.
// The Container class does not have the method :display(), but a
// Container object understands this message and delegates it to the
// contained Item object.

   #include "Hbclass.ch"

   PROCEDURE Main
      LOCAL obj := Item():new( "Hello World" )

      obj := Container():new( "xHarbour", obj )

      ? obj:name             // result: xHarbour
      obj:display()          // result: Hello World

      obj:item := Item():new( "New item" )

      obj:display()          // result: New item
   RETURN


   CLASS Container
      EXPORTED:
      DATA item
      DATA name

      METHOD init(x, o) INLINE (::name := x, ::item := o, self )

      DELEGATE display TO item
   ENDCLASS


   CLASS Item
      EXPORTED:
      DATA name

      METHOD init(x) INLINE (::name := x, self )

      METHOD display INLINE ( QOut(::name), self )
   ENDCLASS

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