| xHarbour Reference Documentation > Statement Reference |
![]() |
![]() |
![]() |
Declares an ASSIGN method of a class.
ASSIGN <MethodName>( <param> ) INLINE <expression>
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 with ACCESS which "simulates" the read access to an instance variable via a method call.
| 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 |
// 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()
? obj:state // result: -1
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 )
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
http://www.xHarbour.com