xHarbour Reference Documentation > Statement Reference xHarbour Developers Network  

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 with ACCESS which "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()

      ? 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

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