xHarbour Reference Documentation > Operator Reference xHarbour Developers Network  

= (assignment)

Simple assignment operator (binary): assigns a value to a variable.

Syntax

<Variable> = <Expression>

Arguments

<Variable>
<Variable> is the name of a variable of any type.
<Expression>
<Expression> is any valid expression whose result is assigned to <Variable>.

Description

The = operator assigns the value of <Expression> to a variable. The recommended assignment operator, however, is the inline assignment operator (:=) which allows for initializing a variable within a variable declaration statement. This is not possible with the simple assignment operator. In addition, the simple assignment operator is interpreted as comparison operator within expressions. This can lead to subtle programming errors since the meaning of the simple assignment operator changes with the context it is used in.

If the variable does not exist when the assignment operation is processed, a PRIVATE variable is automatically created and gets assigned the value of <Expression>.

Info

See also:++, --, :=, = (compound assignment)
Category: Assignment operators , Operators
LIB:xhb.lib
DLL:xhbdll.dll

Example

// The example demonstrates the use of the assignment operator
// and outlines the difference between simple and inline assignment.

   PROCEDURE Main
      LOCAL nMonth

      nMonth = 9                       // simple assignment
      ? nMonth = 9                     // result: .T.

      IF (nMonth := 10) > 0            // inline assignment
         ? "October"                   // output: October
      ENDIF

      IF (nMonth = 9) > 0              // runtime error
         ? "September"                 // (nMonth = 9) -> .F.
      ENDIF
   RETURN

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