Statement VAR Foundation

Declaration of instance variables (member variables of instances of a class).

Syntax
VAR <VarName,...> ;
    [READONLY] [ASSIGNMENT HIDDEN | PROTECTED | EXPORTED] [NOSAVE]
or
VAR <MessageName> [IS <VarName>] [IN <SuperClass>] ;
    [READONLY] [ASSIGNMENT HIDDEN | PROTECTED | EXPORTED] [NOSAVE]
Parameters
<VarName,...>
<VarName,...> is a comma separated list of the names of the member variables which are being declared for instances of this class (instance variables). The name for an instance variable follows the same convention as normal variable names. It must begin with a underscore or a letter and must contain alpha numeric characters. The first 255 characters are significant.
<MessageName>
<MessageName> designates the message which must be sent to an object in order to access the instance variable with the name <VarName>. This defines an alternative name for an instance variable. If <MessageName> is defined, then <MessageName>must be used in the program to access <VarName>. Including the alternate <MessageName> is generally only required when several superclasses are available in the class hierarchy which have instance variables with the same name.
<VarName>
When the option IS is used, a list of variable names cannot be declared. In this case only a single name <VarName> for an instance variable is allowed in the statement VAR.
<SuperClass>
When multiple superclasses have the same instance variable <VarName>it can be specified explicitly from which superclass an instance variable is to be accessed when an object receives the message <MessageName>. Only class variables in a superclass that are visible (which are not declared in the superclass with the attribute HIDDEN:) can be referenced.
READONLY
The option READONLY limits write access for an instance variable. The assignment of a value to an instance variable declared READONLY is only permitted within the source code of methods. If the instance variable has global visibility (visibility attribute EXPORTED:), an assignment can be made within methods of the class and its subclasses. If the visibility attribute is PROTECTED:, READONLY limits the assignment to the methods of the declared class.
ASSIGNMENT
Using the optional ASSIGNMENT clause, write access for an instance variable can be defined independent of its visibility. When the assignment is defined as HIDDEN, assignment is permitted only within the source code of the methods in the declared class. With PROTECTED the assignment is permitted in the source code of methods of the declared class and its subclasses. With EXPORTED, assignment is permitted from anywhere in the source code. EXPORTED is the default setting.
NOSAVE
The option NOSAVE defines an instance variable as not persistent. If an object is transformed to its binary representation with Var2Bin() and converted back to an object using Bin2Var(), these instance variables contain NIL when Bin2Var() returns.
Description

The statement VAR is used within the class declaration (between the CLASS...ENDCLASS statements). It declares a member variable as an instance variable. An instance variable is a member variable of instances of a class. All instances (objects) of a class have a separate set of the instance variables with the variables for each instance containing independent values. Note that an instance variable differs from a class variable which exists only once per class and whose value is the same for all objects of a class. When an instance object receives the message for a class variable, it forwards the message on to the class object.

With <MessageName> the message for accessing an instance variable can be defined. It represents an alternative name for the instance variable. If a <MessageName is defined, it must be used to access the variable <VarName>.

<MessageName> can be included when the instance variable does not exist in the declared class, but in one of its superclasses. When two or more superclasses exist which have the same names for instance variables, it is recommended that unique alternative names be defined for accessing the instance variables of each superclass. In this case the declaration is used in the form CLASS VAR..IS..IN.

The visibility of an instance variable is determined by the visibility attribute set with one of the statements EXPORTED:, PROTECTED: or HIDDEN: (see the section on each visbility attribute for more information). Write access is limited by the options READONLY or ASSIGNMENT independent of the visibility of an instance variable. The following table shows the meaningful combinations between visibility and assignment:

Visibility and assignment

Within a class the name for a member variable (instance variable or class variable) name must be unique. The name of a member variable can be the same as the name of a method, since parentheses must always be included with the call to a method. When no parentheses are used with the message send, the member variable is accessed.

Examples
VAR implementation
// The example illustrates the general procedure for the 
// declaration and the initialization of instance variables. 

CLASS classA                        // Class declaration 
   EXPORTED: 
   VAR    varA1, varA2 
   METHOD init                      // Declares method :init() 
ENDCLASS 

METHOD init( n1, n2 )               // Program the method 
   ::varA1 := n1                    // Assigns beginning values 
   ::varA2 := n2                    // to the instance variables 
RETURN self 

Feedback

If you see anything in the documentation that is not correct, does not match your experience with the particular feature or requires further clarification, please use this form to report a documentation issue.