Commands and Statements

Statement METHOD (Implementation) Foundation

Prefaces the source code for a method of a class

METHOD [<ClassName>:] <MethodName> [( [<Parameters,...>] )]
<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> is the name of the declared method whose source code is being implemented. It must be identical with a method name in the class declaration.
<Parameters,...> is a comma separated list which contains the names of the formal method parameters. Variables passed to the method are LOCAL to the method. These variables are assigned the values passed to the method when it is called.

When the declaration METHOD is positioned in the source code outside of the class declaration (outside the CLASS...ENDCLASS statements), it introduces the source code for a declared method. Methods are programmed like procedures or user defined functions, but they are called differently. An object of the applicable class must first be created by calling the class method :new(). The method name is then sent to this object using the send operator (the colon). The object executes the source code of the method.

Just like functions or procedures, methods can receive arguments whose values are assigned to the variables declared in the parameter list <Parameters,...>. These variables are LOCAL and are visible only within the source 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. Within methods the variable self can neither be explicitly declared nor have a value assigned to it.

Instead of sending a message to the object with self:, the operator :: can be used. The operator :: represents an abbreviation for self: and can be used to access member variables or to call methods of the object.

As with functions and procedures the source code of a method ends with the RETURN statement and the object returns the RETURN value. When a method has no other meaningful RETURN value, self should be returned. In this way, a chained set of messages to an object is possible (for example, object:method1():method2():method3()).

METHOD implementation
// Class declaration 
CLASS Label 
  EXPORTED:                         // Globally visible 
     VAR     nRow, nCol, cLabel, cColor 
     METHOD  init, Show 
// Initializes object with default values when 
// no arguments were passed to with Label():new() 
METHOD Label:init( nRow, nCol, cLabel, cColor ) 
   ::nRow   := IIF( nRow == NIL, Row(), nRow ) 
   ::nCol   := IIF( nCol == NIL, Col(), nCol ) 
   ::cLabel := IIF( cLabel==NIL, "" , cLabel ) 
   ::cColor := IIF( cColor==NIL, SetColor(), cColor ) 
RETURN self 
// Displays label 
METHOD Label:Show 
   @ ::nRow, ::nCol SAY ::cLabel COLOR ::cColor 
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.