xHarbour Reference Documentation > Statement Reference |
Declares an instance variable of a class.
DATA <MemberName> [ INIT <expression> ] ; [ READONLY ] [ PERSISTENT ]
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()).
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 |
// 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
http://www.xHarbour.com