Statement CLASS METHOD (Implementation) Foundation

Prefaces the actual code for a class method.

Syntax
CLASS METHOD [<ClassName>:] <MethodName> [( [<Parameters,...>] )]
Parameters
<ClassName>
<ClassName>: is the name of the class in which the class method is declared. When only one class is declared in the PRG file, the class name is optional. Otherwise<ClassName>: is required.
<MethodName>
<MethodName> is the name of the declared class method whose program code is being implemented. It must be identical with a class method name in the class declaration (declared with CLASS METHOD within the CLASS...ENDCLASS statements).
<Parameters,...>
<Parameters,...> is a comma separated list which contains the names of the formal parameters of the method. These formal parameters declare variables that are LOCAL to the method. When the method is called, the passed parameters are assigned to these variables.
Description

When the declaration CLASS METHOD is outside the class declaration (outside the CLASS...ENDCLASS statements), it introduces the program code for a declared class method. Class methods are programmed like procedures or user defined functions, but they are called somewhat differently. The class object must first be created by a call to the class function. The name of a method is sent to this class object as a message using the send operator which is the colon (:). The class object then executes the source code of the class method.

If an instance of a class receives the message <MethodName>, it forwards this message on to the class object which executes the class method. An instance (object) is produced by the method :new() of the class object.

Just like functions and procedures, methods can receive arguments whose values are assigned to the variables declared in the parameter list <Parameters,...>. These variables are LOCAL to the method and are only visible within the code of the method. A special feature of methods (as opposed to functions) is the LOCAL variable self, which implicitly exists within each method. It always references the object executing the method. The variable self can neither be explicitly declared within methods nor can it have a value assigned to it.

Instead of using the name of the implicit variable self:, the operator :: can be used. The operator :: is an abbreviation for self: and can be used to access class variables or call class methods.

As with functions and procedures, the source code of a class method is closed by the RETURN statement and the object returns the RETURN value. When a method has no other meaningful RETURN value, self should be returned. This allows a chained set of messages to be sent to the class object (for example, object:method1():method2():method3()).

Each class has an implicit class method :initClass(). If this method is declared and implemented, it is executed automatically after the call to the class function. The method :initClass()is used to initialize member variables of the class object.

Examples
Implementing a CLASS METHOD
// 
// Class declarations with class variables and methods 
// 
CLASS Cursor 
   EXPORTED:                        // globally visible 
      CLASS VAR nMaxRow, nMaxCol 

      CLASS METHOD initClass        // Declares 
      CLASS METHOD SetMode, Hide    // class methods 

      VAR    nRow, nCol, nShape 
      METHOD init, Show, UpdatePos 
ENDCLASS 

CLASS METHOD Cursor:initClass()     // Program for 
   ::nMaxRow := MaxRow()            // class methods 
   ::nMaxCol := MaxCol() 
RETURN self 

CLASS METHOD Cursor:SetMode( nMaxRow, nMaxCol ) 
   IF SetMode( nMaxRow, nMaxCol ) 
      ::initClass() 
   ENDIF 
RETURN self 

CLASS METHOD Cursor:Hide 
   SetCursor( 0 ) 
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.