DEFINE CLASS Command - Function or Procedure Definition Clause
Defines method and event functions and procedures for the class definition.
[[PROTECTED | HIDDEN] FUNCTION | PROCEDURE Name[_ACCESS |_ASSIGN]
([cParamName | cArrayName[] [AS Type][@]]) [AS Type]
[HELPSTRING cHelpString] | THIS_ACCESS(cMemberName) [NODEFAULT]
cStatements
[ENDFUNC | ENDPROC]]
Parameters
[[PROTECTED | HIDDEN] FUNCTION | PROCEDURE Name[_ACCESS | _ASSIGN]
Specifies events and methods to create for the class definition. Events and methods are created as functions or procedures. You can create an event function or procedure to respond to an event. For more information, see User-Defined Procedures and Functions, FUNCTION Command, and PROCEDURE Command.Note
Calling event functions and procedures execute the code defined for those functions and procedures and does not call the event itself.
For more information about events in Visual FoxPro, see Understanding the Event Model.
You can create a method function or procedure, which acts on the object created with the class definition.
Note
You must declare each protected method on a separate line.
The _ACCESS or _ASSIGN suffixes specify to create an Access or Assign method for a property with the same name. By default, Access and Assign methods are protected, so you cannot access or make changes to an Access or Assign method from outside of the class. For more information, see Access and Assign Methods and How to: Create Access and Assign Methods.
Note
Arrays are passed to Access and Assign methods in the same manner as standard Visual FoxPro procedures. For more information, see Passing Data to Parameters.
([ cParamName| cArrayName[] [AS Type][@]]) [AS Type]
Specifies one or more parameters to use for passing arguments to the class method or event or specifies the name of an array used to create a type library.Note
When specifying an array, you must use brackets ([]) notation. DEFINE CLASS does not recognize parentheses (()) notation.
For parameters, the first AS Type clause specifies the data type of the parameter.
For arrays, the first AS Type clause specifies a type that must be a valid COM data type. For more information, see the table in the Remarks section. The type can also be a reference to a COM CoClass ProgID such as ADODB.RecordSet. You can specify ProgIDs with or without quotation marks (""). If you use a valid COM CoClass ProgID for an AS Type clause, Visual FoxPro includes it in the type library. For example, the following method defintion:
PROCEDURE Test(oRS AS ADODB.Recordset @) AS ADODB.Recordset
Creates an entry in the type library:
Recordset Test([in, out] Recordset** oRS);
Note
For arrays, you cannot specify a reference to a ProgID that is generated from the type library. For example, you cannot reference a class that is contained in the same COM server.
If you specify an array with a type for the first AS Type clause, Visual FoxPro creates a SAFEARRAY with the specified type. If you specify an array without a type for the first AS Type clause, Visual FoxPro creates a SAFEARRAY of Variant data types. If you specify an invalid type, Visual FoxPro defaults to using Variant type. For more information about safe arrays, see the Remarks section.
The at sign (@) specifies that arguments or arrays are passed to the function or procedure by reference.
Note
By default, data pass to parameters in user-defined procedures by reference and to user-defined functions by value. To pass an entire array, you must pass it by reference. For more information, see Passing Data to Parameters.
Note
You can use the first AS clause to implement strong typing, make IntelliSense functionality available, and for class definition information stored in a type library (OLEPUBLIC). However, Visual FoxPro does not enforce type checking during compilation or code execution so you must make sure to use valid data types. For arrays, strong typing is used primarily for type library creation and is not enforced at run time. By using the cArrayName[] [AS Type][@] clause, you can specify strong typing for arrays so they can be correctly written as safe arrays to a type library. Strict typing is also recommended for use with the interface methods specified by the IMPLEMENTS clause. For more information, see How to: Implement Strong Typing for Class, Object, and Variable Code.
The second AS Type clause indicates the type of the function's return value.
Note
You cannot specify arrays as return types. For example, while the following code is valid, it writes out the return type of the type library as VARIANT and not a SAFEARRAY:
PROCEDURE GetWidgets() AS aWidgets[] ENDPROC
Tip
If the method does not return a value, use AS VOID as the return value. This is required for certain technologies such as Microsoft COM+ Services Queued Components.
If you want parameters and their types to appear in the type library, you must use inline parameter syntax instead of the LPARAMETERS command to declare the parameters, for example:
FUNCTION myMeth(parm1 AS Integer @, parm2 AS String) AS Integer ENDFUNC
[HELPSTRING cHelpString]
Specifies a string to add to the type library as a description of the method's functionality to display in an object browser or IntelliSense.THIS_ACCESS( cMemberName)
Specifies to create a THIS_ACCESS procedure or function to execute when the value of an object member changes or is queried. For more information, see Access and Assign Methods and How to: Create Access and Assign Methods.[NODEFAULT]
Prevents Visual FoxPro from performing default event or method processing for Visual FoxPro events and methods. For more information, see NODEFAULT Command.cStatements
Specifies the code to execute when calling the function or procedure for the class event or method.Tip
You can specify that event and method functions and procedures can accept values by including a PARAMETERS or LPARAMETERS statement as the first executable line of the function or procedure. For more information, see PARAMETERS Command and LPARAMETERS Command.
[ENDFUNC | ENDPROC]]
Indicates the end of the function or procedure. Unlike most Visual FoxPro keywords, you cannot abbreviate ENDFUNC and ENDPROC because they can conflict with the with the ENDFOR and ENDPRINTJOB keywords.
Remarks
The following code shows a summary of the main clauses of the DEFINE CLASS command:
DEFINE CLASS Clause
[Property_Definition_Clause]
[PEMName_COMATTRIB Clause]
[ADD OBJECT Clause]
[IMPLEMENTS Clause]
[Function_Procedure_Definition_Clause]
ENDDEFINE
For more information and full syntax, see DEFINE CLASS Command. For more information about a particular clause of the DEFINE CLASS command, see the following topics:
Examples
Example 1
The following example creates a class named MyForm from the Form base class and contains a procedure for a Click event definition. The form created from the class contains a Click method that displays a dialog box when you click the form.
DEFINE CLASS MyForm AS Form
PROCEDURE Click
= MESSAGEBOX('MyForm has been clicked!')
ENDPROC
ENDDEFINE
Example 2
The following example shows how you can specify strong typing using PROCEDURE cArrayName[] [AS Type ][@][AS Type] clause so that arrays can be correctly written as safe arrays to a type library:
DEFINE CLASS mySession AS Session OLEPUBLIC
PROCEDURE GetWidgets1(aWidgets[])
ENDPROC
PROCEDURE GetWidgets2(aWidgets[] AS Integer)
ENDPROC
PROCEDURE GetWidgets3(aWidgets[] AS Integer @)
ENDPROC
PROCEDURE GetRS(oRS[] AS ADODB.Recordset @)
ENDPROC
ENDDEFINE
As another example, the following code demonstrates how you can specify strong complex typing by defining a type based on a COM class:
DEFINE CLASS mySession AS Session OLEPUBLIC
REFERENCE "ADODB.Recordset"
PROCEDURE GetRS() AS ADODB.Recordset
x=CREATEOBJECT("ADODB.Recordset")
RETURN X
ENDPROC
PROCEDURE SetRS(oRS AS ADODB.Recordset @)
oRS=CREATEOBJECT("ADODB.Recordset")
ENDPROC
ENDDEFINE