xHarbour Reference Documentation > Statement Reference |
Adds a new member variable to an existing class.
EXTEND CLASS <ClassName> WITH DATA <MemberName> [ PERSISTENT ]
The EXTEND CLASS...WITH DATA statement allows for adding new instance variables to a declared class outside the class declaration. This way, a class can be enhanced even if the source code of the class is not available. The EXTEND CLASS statemend can modify all classes existing in xHarbour, except for scalar classes. Scalar classes cannot be extended with data, since a scalar object is the data.
Note: an alternative for enhancing an existing class is to use the FROM option of the CLASS statement and create a sub-class of an existing class.
See also: | CLASS, EXTEND CLASS...WITH METHOD, METHOD...OPERATOR, OPERATOR, OVERRIDE METHOD |
Category: | Class declaration , Declaration , xHarbour extensions |
Header: | hbclass.ch |
Source: | vm\classes.c |
LIB: | xhb.lib |
DLL: | xhbdll.dll |
// The example adds an instance variable to the "root" class of // xHarbour: HBObject(). All new classes inherit from this class. // As a result, the user-defined Test() class inherits this // new member, although :extraData is neither declared in // HBObject() nor in Test(). #include "HbClass.ch" PROCEDURE Main LOCAL obj EXTEND CLASS HBObject WITH DATA extraData obj := Test():new( "xHarbour" ) ? obj:extraData := "a new data slot for all classes" RETURN CLASS Test DATA value METHOD init(x) INLINE (::value := x, self) ENDCLASS
http://www.xHarbour.com