• No results found

Statement Reference

In document xHarbour Language Reference Guide (Page 84-92)

(struct)

(struct)

Creates a new structure object

Syntax

<var> := (struct <StructureName>) or

<var> := (struct <StructureName>*) <pointer>

Arguments

<var>

This is the name of a memory variable to assign the structure object to.

<StructureName>

This is the symbolic name of a structure declared withtypedef struct.

<pointer>

This is the name of a memory variable holding a pointer. The pointer holds the memory address of the structure. The resulting structure object is initialized with the structure data pointed to by

<pointer>.

Description

The (struct) statement provides the most convenient way of creating a structure object of a declaredC structure. (struct) is not a statement in its strongest definition, since it is used on the right side of the assignment operator and produces a return value: a new C Structure object. The name of a declared structure <StructureName> must be provided with the (struct) statement. A corresponding structure object is then instantiated and assigned to the memory variable <var>. This structure object maintains an uninitialized C structure.

When the (struct) statement is coded with an asterisk, an additional memory variable <pointer> must follow the closing brace. This variable must be of Valtype()=="P", i.e. it is a pointer holding the memory address of structure data. The created structure object is then initialized with the structure data stored at the memory address of <pointer>. This is required in the special case when an external function called viaDllCall()produces a pointer to a new structure. Data pointed to by <pointer> is then transferred into a new xHarbour structure object.

Note: refer to thetypedef structstatement for a usage example of (struct).

Info

See also: C Structure class,DllCall(),pragma pack(),typedef struct Category: C Structure support,xHarbour extensions

Header: cstruct.ch, winapi.ch, wintypes.ch Source: rtl\cstruct.prg

LIB: xhb.lib

DLL: xhbdll.dll

ACCESS

ACCESS

Declares an ACCESS method of a class.

Syntax

ACCESS <MethodName> [ INLINE <expression> | VIRTUAL ]

Arguments

<MethodName>

This is the symbolic name of the access method to implement. It must begin with a letter or underscore followed by digits, letters or underscores. The symbolic name can contain up to 63 characters.

INLINE <expression>

Normally, access methods are implemented outside theclass declaration, using a regular METHOD (implementation). When an access method is implemented as INLINE method,

<expression> is executed when the access method is invoked. <expression> must be one line of code. The code cannot contain commands but only function and method calls.

VIRTUAL

An access method can be declared asVIRTUALmethod. The VIRTUAL and INLINE clauses are mutually exclusive.

Description

An ACCESS method is a special method since it is invoked when an object receives a message "as if"

an instance variable is queried. Sending <MethodName> without including parentheses to an object results in the method call. ACCESS methods "simulate" an instance variable for the calling context.

An object can have "computed" instance variables whose values are the result of a method call. An ACCESS method "hides" the method call from the calling context.

Another special method type can be declared with ASSIGNwhich "simulates" the write access to an instance variable via a method call.

Info

See also: ASSIGN,CLASS,DATA,EXPORTED:,METHOD (declaration), METHOD...VIRTUAL

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 that uses ACCESS methods // for simulating instance variables.

#include "hbclass.ch"

PROCEDURE Main

ACCESS LOCAL obj := Someday():new()

// real instance variable

? obj:date // result: 08/31/06 // "simulated" instance variables

? obj:yesterday // result: Wednesday

? obj:today // result: Thursday

? obj:tomorrow // result: Friday

? obj:nextWeek // result: 09/04/06 RETURN

CLASS Someday EXPORTED:

DATA date INIT Date() ACCESS today

ACCESS tomorrow ACCESS yesterday ACCESS nextWeek ENDCLASS

METHOD today

RETURN CDoW( ::date ) METHOD tomorrow

RETURN CDoW( ::date + 1 ) METHOD yesterday

RETURN CDoW( ::date - 1 ) METHOD nextWeek

LOCAL nDays := -1 // find next Monday

DO WHILE DoW( ++nDays + ::date ) <> 2 ENDDO

RETURN ::date + nDays

ANNOUNCE

ANNOUNCE

Declaration of a module identifier name.

Syntax

ANNOUNCE <ModuleId>

Arguments

<ModuleId>

<ModuleId> is the symbolic name of a module.

Description

The ANNOUNCE statement declares a symbolic name for the linker which is not declared as FUNCTION, PROCEDURE or CLASS in the PRG code of an xHarbour project. This way, an entire module can be assigned a symbolic name that is later resolved by the linker.

ANNOUNCE must appear prior to any executable statement in a PRG source code file. The

<ModuleID> identifier must be unique for the entire xHarbour project.

Info

See also: #include,EXTERNAL,REQUEST Category: Declaration,Statements

Examples

// The example shows how the default replaceable database driver // is linked into an application.

ANNOUNCE RDDSYS REQUEST _DBF REQUEST DBFNTX REQUEST DBFFPT PROCEDURE Main

? "This program has the default RDD linked in"

RETURN

// The example demonstrates how to produce an executable that does not // require/use a replaceable database driver.

ANNOUNCE RDDSYS PROCEDURE Main

? "This program has no RDD linked in"

RETURN

ASSIGN

ASSIGN

Declares an ASSIGN method of a class.

Syntax

ASSIGN <MethodName>( <param> ) INLINE <expression>

Arguments

<MethodName>

This is the symbolic name of the assign method to implement. It must begin with a letter or underscore followed by digits, letters or underscores. The symbolic name can contain up to 63 characters.

( <param> )

An access method receives exactly one parameter which must be declared in parentheses.

INLINE <expression>

ACCESS methods must be declared and implemented as INLINE methods. <expression> is executed when the access method is invoked. It must be one line of code which cannot contain commands but only function and method calls.

Description

An ASSIGN method is a special method since it is invoked when a value is assigned to an instance variable of an object. Instead of the assignment operation, the access method is invoked and the assigned value is passed as parameter to the method where it is processed. ASSIGN methods

"simulate" the assignment of a value to an instance variable for the calling context.

Another special method type can be declared withACCESSwhich "simulates" the read access to an instance variable via a method call.

Info

See also: ACCESS,CLASS,DATA,EXPORTED:,METHOD (declaration) 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 instances have three states // represented as numeric -1, 0 and 1. The state can be changed by // assigning values of different data types.

#include "Hbclass.ch"

#include "Error.ch"

PROCEDURE Main

LOCAL obj := ThreeState():new()

ASSIGN

obj:state := "ON"

? obj:state // result: 1

obj:state := "OFF"

? obj:state // result: 0

obj:state := "UNDEF"

? obj:state // result: -1

obj:state := .T.

? obj:state // result: 1

obj:state := NIL

? obj:state // result: -1

obj:state := 1

? obj:state // result: 1

obj:state := 3 // runtime error RETURN

CLASS ThreeState PROTECTED:

DATA _value INIT -1 EXPORTED:

ACCESS state

ASSIGN state( x ) INLINE ::state( x ) ENDCLASS

METHOD state( xState ) CLASS ThreeState LOCAL lError := .F.

IF PCount() == 0 RETURN ::_value ENDIF

IF Valtype( xState ) == "N"

IF xState IN { -1, 0, 1 } ::_value := xState ELSE

lError := .T.

ENDIF

ELSEIF Valtype( xState ) == "C"

DO CASE

CASE Upper( xState ) == "ON"

::_value := 1

CASE Upper( xState ) == "OFF"

::_value := 0

CASE Upper( xState ) == "UNDEF"

::_value := -1 OTHERWISE

lError := .T.

ENDCASE

ELSEIF Valtype( xState ) == "L"

::_value := IIF( xState, 1, 0 )

ASSIGN

ELSEIF Valtype( xState ) == "U"

::_value := -1 ELSE

lError := .T.

ENDIF IF lError

RETURN ::error( "illegal data assigned", ; ::className(), "state" , ;

EG_ARG, {xState} )

ENDIF

RETURN ::_value

In document xHarbour Language Reference Guide (Page 84-92)