Statement CLASS METHOD (Declaration) Foundation

Declaring class methods (methods of the class object)

CLASS METHOD <MethodName,...>
CLASS METHOD <MessageName> [IS <MethodName>] [IN <SuperClass>]
<MethodName,...> is a comma separated list containing names of class methods of the class object being declared. The name for a method follows the same convention as function and variable names. It must begin with an underscore or a letter and must contain alpha numeric characters. The first 255 characters are significant.
<MessageName> designates the message which must be sent to a class object so that it executes the class method with the name <MethodName>. <MessageName> is used when several superclasses are available in the class hierarchy which have methods with the same name.
When the option IS is used, a list of method names cannot be used. In this case only a single method name is permitted in the CLASS METHOD declaration. The method with the name <MethodName> is executed by the class object when it receives the message <MessageName>.
When multiple superclasses have the same class method <MethodName>it can be specified explicitly from which superclass a class method is to be called when an object receives the message <MessageName>. Only class methods of a superclass which are visible (which are not declared with the attribute HIDDEN: within the superclass declaration) can be referenced.

CLASS METHOD declares a method as a class method within the class declaration (CLASS...ENDCLASS). Class methods are executed only from the class object. When an instance object receives the message for a class method, it forwards the message on to the class object.

The source code for a class method is generally implemented after the statement ENDCLASS. The code for the class method is prefaced by the declaration CLASS METHOD <MethodName>. If the class method is already implemented in a superclass or is assigned to a specific superclass by the option IN, the class method does not need to be implemented within this class. Instead, an object of this class executes the corresponding method in the superclass. When the class method <MethodName> is implemented in a superclass, it is automatically available for use in a subclass. When a class method of the same name is also declared in the subclass, the object executes the class method within the subclass and not the class method of the same name which exists in a superclass.

Within the source code for the class method the class object is always available via the variable self. This occurs whether the message to call the class method was sent to an instance object or directly to the class object.

When using CLASS METHOD outside the class declaration to preface the method implementation, the name of the class method <MethodName> is always used. When the method is declared as <MessageName> IS <MethodName>, <MessageName> determines the message which must be sent to an object, for the method <MethodName> to be executed.

<MessageName> is a mechanism for providing an alternative name for a class method. It can be used to declare a different name in this class than that used in the superclass. This is especially useful when two or more superclasses contain class methods with the same name. When this occurs, declaring alternative message names (unique within this class) for calling the class method in each of the superclasses is recommended. This clearly indicates which superclass class method will be used. To accomplish this, the declaration is used in the form CLASS METHOD..IS..IN.

The visibility of a class method is determined by the visibility attribute, which is set with one of the keywords EXPORTED:, PROTECTED:, or HIDDEN: (see the sections covering each attribute for additional information).

In general, class methods are only needed for accessing class variables. Class variables are member variables of the class object, and exist only once within a class. The value or contents of a class variable is the same for all instances of a class.

Reserved names: The object model of Xbase++ uses the following identifiers for class methods which are reserved and cannot be used in naming class methods. These are:

Reserved identifiers for class methods
Method Description
:new() Creates instance
:className() Returns name of class
:classObject() Returns class object
:isDerivedFrom() Determines whether or not an object is derived from a particular class :isDerivedFrom(<cClassName> | <oClassObject>) --> .T.|.F.

Declaring a CLASS METHOD
// Class declaration with class variables and methods 
CLASS Cursor 
   EXPORTED:                        // globally visible 
      CLASS VAR nMaxRow, nMaxCol 

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

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

CLASS METHOD Cursor:initClass()     // class method 
   ::nMaxRow := MaxRow()            // implementations 
   ::nMaxCol := MaxCol() 
RETURN self 

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

   SetCursor( 0 ) 
RETURN self 

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.