Difference between revisions of "CLASS - Methods"

From Recital Documentation Wiki
Jump to: navigation, search
 
 
(15 intermediate revisions by 2 users not shown)
Line 1: Line 1:
=CLASS - Methods=
 
 
 
==Class==
 
Objects
 
 
 
 
==Purpose==
 
==Purpose==
 
Define a method in a user-defined class
 
Define a method in a user-defined class
Line 17: Line 10:
  
 
==See Also==
 
==See Also==
[[ADDPROPERTY()]], [[CLASS]], [[CLASS – PARAMETERS]], [[CLASS – PROPERTIES]], [[CLASS – SCOPING]], [[DEFINE CLASS]], [[CREATEOBJECT()]], [[DODEFAULT()]],[[METHOD]], [[NEWOBJECT()]], [[REMOVEPROPERTY()]]
+
[[ACLASS()]], [[ADDPROPERTY()]], [[AMEMBERS()]], [[CLASS]], [[CLASS - Parameters]], [[CLASS - Properties]], [[CLASS - Scoping]], [[COMPOBJ()]], [[CREATEOBJECT()]], [[DEFINE CLASS]], [[DISPLAY CLASSES]], [[DODEFAULT()]], [[FOREACH]], [[LIST CLASSES]], [[LOADOBJECT()]], [[METHOD]], [[NEWOBJECT()]], [[OBJECT()]], [[PRINT_HTML()]], [[PRINT_JSON()]], [[PRINT_R()]], [[PRINT_XML()]], [[REMOVEPROPERTY()]], [[REQUIRE_ONCE()]], [[SAVEOBJECT()]], [[SQL SELECT]], [[WITH]]
  
  
Line 23: Line 16:
 
An object encapsulates properties and all of the methods that perform operations on the object.  Encapsulation hides data within an object, and makes an object into a full self-contained operational unit.  A class method is a self-contained function defined in the class, accessible only through an instantiation of the class.
 
An object encapsulates properties and all of the methods that perform operations on the object.  Encapsulation hides data within an object, and makes an object into a full self-contained operational unit.  A class method is a self-contained function defined in the class, accessible only through an instantiation of the class.
  
You define the methods of a class using the METHOD command in the CLASS...ENDCLASS construct.  The 'This.' operator is used to reference properties of the active object from within its methods.  Parameters can be passed to a method, the PARAMETER statement can be used to define the parameters in the method.
+
You define the methods of a class using the METHOD command in the CLASS...ENDCLASS construct.  The ''this.'' operator is used to reference properties of the active object from within its methods.  Parameters can be passed to a method, the PARAMETER statement can be used to define the parameters in the method.
  
 
====<method name>====
 
====<method name>====
The <method name> must be unique name of up to 32 characters.  The method is called using the "object.method()" syntax.  You can create special methods in the class.  When a new object is created, and the class contains a method called CONSTRUCTOR, that method is called to complete the process of creating the object.  You can pass parameters to the CONSTRUCTOR method when you create the new object.  The PARAMETER statement must be specified in the method for accepting parameters.  When the object is released, and it contains a method called DESTRUCTOR, that method is called prior to the object storage being released.  The Visual FoxPro equivalents are also supported: INIT for CONSTRUCTOR and DESTROY for DESTRUCTOR.  When a property that has been defined with the NOTIFY clause is read, and the class contains a method called GETPROPERTY, that method is called.  The property name is passed as an upper case character string to the GETPROPERTY method as a parameterWhen a property that has been defined with the NOTIFY clause is updated, and the class contains a method called SETPROPERTY, that method is called. The property name, in upper case, and the new value are passed to the SETPROPERTY method as parameters.
+
The <method name> must be unique name of up to 32 characters.  The method is called using the "object.method()" syntax.  You can create special methods in the class:
 +
 
 +
 
 +
{| class="wikitable"
 +
|-
 +
!Name!!Description
 +
|-
 +
|init||Called after an object is first createdThis is a known as the constructor method and may also be named ''constructor''.
 +
|-
 +
|destroy||Called just prior to an object being destroyed.  This is known as the destructor method and may also be named ''destructor''.
 +
|-
 +
|<property>_access||Property access notification for the property <property>.
 +
|-
 +
|<property>_assign||Property assignment notification for the property <property>.
 +
|-
 +
|this_access||Property access notification.  This will be called for any property which does not have its own <property>_access method.  May also be named ''getproperty''.
 +
|-
 +
|this_assign||Property assignment notification.  This will be called for any property which does not have its own <property>_assign method.  May also be named ''setproperty''.
 +
|-
 +
|error||Class specific error handler.
 +
|}
 +
 
 +
 
 +
When a new object is created, and the class contains a method called ''init'', that method is called to complete the process of creating the object.  You can pass parameters to the init constructor method when you create the new object.  The [[PARAMETERS]] statement must be specified in the method for accepting parameters.  When the object is released, and it contains a method called ''destroy'', that method is called prior to the object storage being released.   
 +
 
 +
When a property is accessed and before its value is returned, a check is made to see if a method named ''<property>_access'' exists, where <property> is the name of the property.  If this method exists, it is calledIf not, a check is made to see if there is a method named ''this_access'' and it is called if it exists.
 +
 
 +
Similarly, after a new value is assigned to a property, checks are made for ''<property>_assign'' and ''this_assign''.
  
 
====EXTERNAL====
 
====EXTERNAL====
Line 33: Line 53:
 
====RETURN====
 
====RETURN====
 
The RETURN clause is used to specify the end of the method definition.
 
The RETURN clause is used to specify the end of the method definition.
 +
 +
====PROCEDURE...ENDPROC====
 +
The [[PROCEDURE|PROCEDURE...ENDPROC]] syntax may also be used to define a method.
  
  
Line 39: Line 62:
 
// Example of External Method
 
// Example of External Method
 
class Box
 
class Box
method DrawFrame external
+
    method DrawFrame external
 
endclass
 
endclass
  
 
Method Box::DrawFrame
 
Method Box::DrawFrame
parameters nX1, nY1, nX2, nY2, cFGCOL, cBGCOL
+
    parameters nX1, nY1, nX2, nY2, cFGCOL, cBGCOL
@nX1,nY1 clear to nX2,nY2
+
    @nX1,nY1 clear to nX2,nY2
@nX1,nY1 fill to nX2,nY2 ;
+
    @nX1,nY1 fill to nX2,nY2 ;
color &(cFGCOL + "/" + cBGCOL)
+
      color &(cFGCOL + "/" + cBGCOL)
@nX1,nY1 to nX2,nY2 ;
+
    @nX1,nY1 to nX2,nY2 ;
color &(cFGCOL + "/" + cBGCOL)
+
      color &(cFGCOL + "/" + cBGCOL)
 
return  && DrawFrame
 
return  && DrawFrame
  
Line 54: Line 77:
 
oDIALOGOK.DrawFrame(5,25,12,54, "R", "Gr")
 
oDIALOGOK.DrawFrame(5,25,12,54, "R", "Gr")
  
// Example of Visual FoxPro Method names
+
// Example of constructor and destructor
  
 
class Box  
 
class Box  
procedure Draw  
+
    procedure Draw  
  messagebox("This is the parent Draw Method")  
+
        messagebox("This is the parent Draw Method")  
endproc && Draw  
+
    endproc && Draw  
 
endclass  
 
endclass  
  
 
class Dialog1 of Box  
 
class Dialog1 of Box  
procedure Init
+
    procedure Init
  messagebox("This is the object Init Method")  
+
      messagebox("This is the object Init Method")  
endproc
+
    endproc
procedure Destroy
+
    procedure Destroy
  messagebox("This is the object Destroy Method")  
+
      messagebox("This is the object Destroy Method")  
endproc
+
    endproc
procedure Draw  
+
    procedure Draw  
  messagebox("This is the object Draw Method")  
+
      messagebox("This is the object Draw Method")  
  dodefault()  
+
      dodefault()  
endproc && Draw  
+
    endproc && Draw  
 
endclass  
 
endclass  
  
Line 84: Line 107:
  
 
==Products==
 
==Products==
Recital Database Server, Recital Mirage Server, Recital Terminal Developer
+
Recital Server, Recital  
 
[[Category:Documentation]]
 
[[Category:Documentation]]
 
[[Category:Commands]]
 
[[Category:Commands]]
 +
[[Category:Objects]]
 +
[[Category:Objects Commands]]

Latest revision as of 11:34, 13 January 2010

Purpose

Define a method in a user-defined class


Syntax

METHOD <method name> [EXTERNAL]

RETURN


See Also

ACLASS(), ADDPROPERTY(), AMEMBERS(), CLASS, CLASS - Parameters, CLASS - Properties, CLASS - Scoping, COMPOBJ(), CREATEOBJECT(), DEFINE CLASS, DISPLAY CLASSES, DODEFAULT(), FOREACH, LIST CLASSES, LOADOBJECT(), METHOD, NEWOBJECT(), OBJECT(), PRINT_HTML(), PRINT_JSON(), PRINT_R(), PRINT_XML(), REMOVEPROPERTY(), REQUIRE_ONCE(), SAVEOBJECT(), SQL SELECT, WITH


Description

An object encapsulates properties and all of the methods that perform operations on the object. Encapsulation hides data within an object, and makes an object into a full self-contained operational unit. A class method is a self-contained function defined in the class, accessible only through an instantiation of the class.

You define the methods of a class using the METHOD command in the CLASS...ENDCLASS construct. The this. operator is used to reference properties of the active object from within its methods. Parameters can be passed to a method, the PARAMETER statement can be used to define the parameters in the method.

<method name>

The <method name> must be unique name of up to 32 characters. The method is called using the "object.method()" syntax. You can create special methods in the class:


Name Description
init Called after an object is first created. This is a known as the constructor method and may also be named constructor.
destroy Called just prior to an object being destroyed. This is known as the destructor method and may also be named destructor.
<property>_access Property access notification for the property <property>.
<property>_assign Property assignment notification for the property <property>.
this_access Property access notification. This will be called for any property which does not have its own <property>_access method. May also be named getproperty.
this_assign Property assignment notification. This will be called for any property which does not have its own <property>_assign method. May also be named setproperty.
error Class specific error handler.


When a new object is created, and the class contains a method called init, that method is called to complete the process of creating the object. You can pass parameters to the init constructor method when you create the new object. The PARAMETERS statement must be specified in the method for accepting parameters. When the object is released, and it contains a method called destroy, that method is called prior to the object storage being released.

When a property is accessed and before its value is returned, a check is made to see if a method named <property>_access exists, where <property> is the name of the property. If this method exists, it is called. If not, a check is made to see if there is a method named this_access and it is called if it exists.

Similarly, after a new value is assigned to a property, checks are made for <property>_assign and this_assign.

EXTERNAL

Methods can be defined outside the CLASS...ENDCLASS construct with the METHOD command. The EXTERNAL clause is used to make an external method known to the CLASS...ENDCLASS construct. When a method is defined externally, its name should be preceded by the keyword METHOD, followed by the Class name, followed by two colon characters (e.g. Method MyClass::ExtMethod).

RETURN

The RETURN clause is used to specify the end of the method definition.

PROCEDURE...ENDPROC

The PROCEDURE...ENDPROC syntax may also be used to define a method.


Example

// Example of External Method
class Box
    method DrawFrame external
endclass
 
Method Box::DrawFrame
    parameters nX1, nY1, nX2, nY2, cFGCOL, cBGCOL
    @nX1,nY1 clear to nX2,nY2
    @nX1,nY1 fill to nX2,nY2 ;
      color &(cFGCOL + "/" + cBGCOL)
    @nX1,nY1 to nX2,nY2 ;
      color &(cFGCOL + "/" + cBGCOL)
return  && DrawFrame
 
oDIALOGOK = new Box() 
oDIALOGOK.DrawFrame(5,25,12,54, "R", "Gr")
 
// Example of constructor and destructor
 
class Box 
    procedure Draw 
        messagebox("This is the parent Draw Method") 
    endproc && Draw 
endclass 
 
class Dialog1 of Box 
    procedure Init
      messagebox("This is the object Init Method") 
    endproc
    procedure Destroy
      messagebox("This is the object Destroy Method") 
    endproc
    procedure Draw 
      messagebox("This is the object Draw Method") 
      dodefault() 
    endproc && Draw 
endclass 
 
oDIALOG = createobject("Dialog1")
oDIALOG.Draw()
oDIALOG.AddProperty("myprop", "hello world") 
messagebox(oDIALOG.myprop)
release oDIALOG


Products

Recital Server, Recital