xHarbour Reference Documentation > Statement Reference xHarbour Developers Network  

DATA

Declares an instance variable of a class.

Syntax

DATA <MemberName> [ INIT <expression> ] ;
                  [ READONLY ] [ PERSISTENT ]

Arguments

<MemberName>
This is the symbolic name of the instance 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 an object is created. The return value of <expression> is assigned to <MemberName>.
READONLY
This option declares an instance variable as read only. I.e. when the object 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.
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 DATA statement can only be used in the class declaration between CLASS and ENDCLASS. It declares the symbolic name of an instance variable. This name must be sent to an object to access the instance variable.

All objects of a class have the same set of instance variables, but two objects can hold different values in their instance variables. They are normally initialized with default values using the INIT clause. The value of <expression> is assigned to an instance variable when the object is created.

An alternative initialization of instance variables is provided with the :init() method (see HBObject()).

Info

See also:ACCESS, ASSIGN, CLASS, EXPORTED:, HBObject(), OVERRIDE METHOD, VAR
Category: Class declaration , Declaration , xHarbour extensions
Header:hbclass.ch
Source:vm\classes.c
LIB:xhb.lib
DLL:xhbdll.dll

Example

// The example implements a class whose objects hold file information.
// Two protected instance variables are used to collect file name and
// the results of function FileStats(). Exported instance variables
// are simulated with ACCESS and ASSIGN methods.

   #include "HbClass.ch"

   PROCEDURE Main
      LOCAL obj := FileInfo():new( "Test.prg" )

      ? obj:fileName           // result: Test.prg
      ? obj:fileSize           // result:       1718
      ? obj:fileAttr           // result: A
      ? obj:createDate         // result: 09/01/06
      ? obj:createTime         // result: 14:08:23

      obj:fileName := "Test2.prg"

      ? obj:fileName           // result: Test2.prg
      ? obj:fileSize           // result:        846
      ? obj:fileAttr           // result: A
      ? obj:createDate         // result: 11/17/05
      ? obj:createTime         // result: 09:44:34
   RETURN


   CLASS FileInfo
      PROTECTED:
      DATA    cFileName    INIT ""
      DATA    aInfo        INIT { "", 0, CtoD(""), 0, CtoD(""), 0 }
      METHOD  getInfo

      EXPORTED:
      METHOD  init

      ACCESS  fileName     INLINE ::cFileName
      ASSIGN  fileName(c)  INLINE ::getInfo(c)

      ACCESS  fileAttr     INLINE ::aInfo[1]
      ACCESS  fileSize     INLINE ::aInfo[2]
      ACCESS  createDate   INLINE ::aInfo[3]
      ACCESS  createTime   INLINE TString( ::aInfo[4] )
      ACCESS  changeDate   INLINE ::aInfo[5]
      ACCESS  changeTime   INLINE TString( ::aInfo[6] )
   ENDCLASS

   METHOD init( cFileName ) CLASS FileInfo
      IF Valtype( cFileName ) == "C"
         ::getInfo( cFileName )
      ENDIF
   RETURN self

   METHOD getInfo( cFileName ) CLASS FileInfo
      IF .NOT. File( cFileName )
         RETURN ::error( "File not found"         , ;
                         ::className(), "getInfo" , ;
                         EG_ARG, {cFileName}        )
      ENDIF

      ::cFileName := cFileName
      FileStats( cFileName, @::aInfo[1], ;
                            @::aInfo[2], ;
                            @::aInfo[3], ;
                            @::aInfo[4], ;
                            @::aInfo[5], ;
                            @::aInfo[6]  )
   RETURN self

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