xHarbour Reference Documentation > Statement Reference xHarbour Developers Network  

CLASSDATA

Declares a class variable of a class.

Syntax

CLASSDATA <MemberName> [ INIT <expression> ] ;
                       [ READONLY ] [ SHARED ]

Arguments

<MemberName>
This is the symbolic name of the class variable to declare. It must begin with a letter or underscore followed by digits, letters or underscores. The symbolic name can contain up to 63 characters.
INIT <expression>
This is the expression which is executed when the class function is called. The return value of <expression> is assigned to <MemberName>.
READONLY
This option declares a class variable as read only. I.e. when an instance exists outside the declaring PRG module, no value can be assigned to <MemberName>. The value of <MemberName> can be changed within methods of the class.
SHARED
This option specifies that the class variable will be shared in all sub-classes of the declared class. If SHARED is omitted, sub-classes receive their own copy of the declared class variable.

Description

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()).

Info

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

Examples

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

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