xHarbour Reference Documentation > Statement Reference |
Declares a class variable of a class.
CLASSDATA <MemberName> [ INIT <expression> ] ; [ READONLY ] [ SHARED ]
The CLASSDATA statement can only be used in the class declaration between CLASS and ENDCLASS. It declares the symbolic name of a class variable. This name must be sent to an object to access the class variable.
Class variables are a special type of member variables since the value of a class variable is the same for all instances of a class. Two objects of a class can hold different values in their instance variables but have the same values in their class variables.
An alternative initialization of class variables is provided with the :initClass() method (see HBObject()).
See also: | ACCESS, ASSIGN, CLASS, DATA, EXPORTED: |
Category: | Class declaration , Declaration , xHarbour extensions |
Header: | hbclass.ch |
Source: | vm\classes.c |
LIB: | xhb.lib |
DLL: | xhbdll.dll |
// The example desmonstrates the nature of class variables using // two objects of the same class. The class variable is changed // with one object. This change is reflected also in the second // object. #include "HbClass.ch" PROCEDURE Main LOCAL objA, objB objA := Test():new( "One" ) objB := Test():new( "Two" ) ? objA:value, objA:config // result: One Initial value ? objB:value, objB:config // result: Two Initial value objB:config := "New value" ? objA:value, objA:config // result: One New value ? objB:value, objB:config // result: Two New value RETURN CLASS Test EXPORTED: CLASSDATA config INIT "Initial value" DATA value METHOD init(x) INLINE ( ::value := x, self ) ENDCLASS
// The example outlines the difference between a SHARED and // an unshared class variable. Class variables are assigned // new values for the SubClass object. The changed value // is only reflected in the SHARED class variable of the // BaseClass object. #include "HbClass.ch" PROCEDURE Main LOCAL objA, objB objA := BaseClass( "First" ):new( "One" ) objB := SubClass ( "Second"):new( "Two" ) ? objA:value, objA:cPrivate, objA:cPublic objB:display() objB:cPrivate := "Hello" objB:cPublic := "World" ? objA:value, objA:cPrivate, objA:cPublic objB:display() // ******** Output ********** // One First Global // Two Second Global // One First World // Two Hello World RETURN // ------------ CLASS BaseClass EXPORTED: CLASSDATA cPrivate CLASSDATA cPublic INIT "Global" SHARED DATA value METHOD initClass METHOD init ENDCLASS METHOD initClass( x ) CLASS BaseClass ::cPrivate := x RETURN self METHOD init( x ) CLASS BaseClass ::value := x RETURN self // -------------------------- CLASS SubClass FROM BaseClass EXPORTED: METHOD display ENDCLASS METHOD display CLASS SubClass ? ::value, ::cPrivate, ::cPublic RETURN self
http://www.xHarbour.com