xHarbour Reference Documentation > Statement Reference xHarbour Developers Network  

EXTEND CLASS...WITH DATA

Adds a new member variable to an existing class.

Syntax

EXTEND CLASS <ClassName> WITH DATA <MemberName> [ PERSISTENT ]

Arguments

<ClassName>
This is the symbolic name of an existing class to add a new instance variable to.
<MemberName>
This is the symbolic name of the new instance variable.
PERSISTENT
This option specifies that the value assigned to <MemberName> is persistent. When an object is serialized with HB_Serialize() and later restored with HB_Deserialize(), the value of <MemberName> is also restored. All instance variables not declared as PERSISTENT are initialized with NIL when a serialized object is restored from its character representation.

Description

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.

Info

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

Example

// 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

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