| xHarbour Reference Documentation > Statement Reference |
![]() |
![]() |
![]() |
Declares a message to be directed to a contained object.
DELEGATE <Message> TO <ContainedObject>
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.
| See also: | ACCESS, CLASS, : |
| Category: | Class declaration , Declaration , xHarbour extensions |
| Header: | hbclass.ch |
| Source: | vm\classes.c |
| LIB: | xhb.lib |
| DLL: | xhbdll.dll |
// 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
http://www.xHarbour.com