Show/Hide Toolbars

XSharp

Purpose

Declare a procedure name and formal parameters. When used inside a FoxPro DEFINE CLASS .. ENDDEFINE this declares a method.

Syntax

 [Attributes] [Modifiers] PROCEDURE  <idProcedure>

 [Typeparameters]

 [([<idParam> [AS | REF|OUT|IN <idType>] [, ...])]

 [AS <idType>]

 [TypeparameterConstraints]

 [<idConvention>]

 [_INIT1 | _INIT2 | _INIT3 | EXIT]

 [EXPORT LOCAL]

 [DLLEXPORT STRING_CONST]

 [=> <expression>]

 CRLF

 [<Body>]

 [ENDPROC | END PROCEDURE]

 

Alternative INIT / EXIT Procedures

 

 [Attributes] [INIT | EXIT] PROCEDURE  <idProcedure>

 [Typeparameters]

 [([<idParam> [AS | REF|OUT|IN <idType>] [, ...])]

 [AS <idType>]

 [TypeparameterConstraints]

 [<idConvention>]

 [=> <expression>]

 CRLF

 [<Body>]

 [ENDPROC | END PROCEDURE]

 

Arguments

AttributesAn optional list of one or more attributes that describe meta information for am entity, such as for example the [TestMethod] attribute on a method/function containing tests in a MsTest class library. Please note that Attributes must be on the same line or suffixed with a semi colon when they are written on the line above that keyword.

 

ModifiersAn optional list of modifiers that specify the visibility or scope of the entity, such as PUBLIC, STATIC, INTERNAL, EXPORT and UNSAFE.
Please note that functions and procedures used as class members in FoxPro compatible classes can have more modifiers.
 
<idProcedure>A valid identifier name for the function.  A function is an entity and, as such, shares the same name space as other entities.  This means that it is not possible to have a function and a class, for example, with the same name.

 

TypeParametersThis is supported for methods with generic type arguments. This something like <T> for a method with one type parameter named T. Usually one of the parameters in the parameter list is then also of type T.

 

<idParam>A  parameter variable.  A variable specified in this manner is automatically declared local.  These variables, also called formal parameters, are used to receive arguments that you pass when you call the entity.

 

AS | REF|OUT|IN <idType>Specifies the data type of the parameter variable (called strong typing).  AS indicates that the parameter must be passed by value, and REF indicates that it must be passed by reference with the @ operator. OUT is a special kind of REF parameter that does not have to be assigned before the call and must be assigned inside the body of the entity. IN parameters are passed as READONLY references.
The last parameter in the list can also be declared as PARAMS <idType>[] which will tell the compiler that the function/method may receive zero or more optional parameters.
Functions or Methods of the CLIPPER calling convention are compiled to a function with a single parameter that this declared as Args PARAMS USUAL[]
 

 

AS <idType>Specifies the data type.  If omitted, then depending on the compiler options the type will be either USUAL or determined by the compiler.

 

TypeParameterConstraintsHere you can specify constraints for the Type parameters, such as WHERE T IS SomeName or WHERE T IS New

 

<idConvention>Specifies the calling convention for this entity.  <idConvention> must be one of the following:

o        CLIPPER

o        STRICT

o        PASCAL

o        CALLBACK

o        THISCALL

Most calling conventions are for backward compatibility only.
There are 2 exceptions:
CLIPPER declares that a method has untyped parameters. This is usually only needed for methods without any declared parameters. Otherwise the compiler will assume CLIPPER calling convention when it detects untyped parameters.
Methods and Functions in external DLL may have STRICT, PASCAL, CALLBACK

 

 

EXPORT LOCALThis clause is allowed by X# but ignored.

 

_INITn, EXITWhen an application is executed, all INIT procedures in all modules associated with the application (including libraries) are called automatically.  There are three priority levels for INIT procedures indicated by the _INIT1, _INIT2, and _INIT3 keywords.  _INIT1 procedures are called first, _INIT2 second, and _INIT3 third.  All INIT procedures are called before the application Start() routine.
EXIT procedures are called at application shutdown
=> <Expression>Single expression that replaces the multiline body for the entity. CANNOT be compiled with a body

 

<Body>Program statements that form the code of this entity.
The <Body> can contain one or more RETURN statements to return control to the calling routine and to serve as the function return value.  If no return statement is specified, control passes back to the calling routine when the function definition ends, and the function will return a default value depending on the return value data type specified (NIL if the return value is not strongly typed).
CANNOT be combined with an Expression Body

 

ENDPROC | END PROCEDUREThese (optional)keywords indicate the logical end of the function.

 

You must follow the guidelines below when specifying an INIT procedure:

       No arguments are allowed

 

Notes

Please not that INIT and EXIT procedures have an INTERNAL scope always. You cannot access these from outside of the assembly where they are defined, so prevent you from accidentally calling them. If you need to call them, then you may consider to store the actual code in a normal function or procedure and call that code from the INIT or EXIT procedure.

Examples

 

PROCEDURE First AS VOID PASCAL _INIT1

 

INIT procedures are necessary for having automatic initialization routines for libraries and other modules in an application besides the main startup module (i.e., the one containing the Start() routine).  Although Start() routines are limited to one per application, there are no inherent limits for the total number of INIT procedures within an application.

 

The following example shows a skeleton of a typical procedure that uses declared variables:

 

PROCEDURE Skeleton(cName, cClassRoom, Bones, nJoints)
 LOCAL nCrossBones, aOnHand := {"skull", "metacarpals"}
 STATIC nCounter := 0        
 
 <Executable Statements>...

 

The next example determines whether an argument was skipped by comparing the parameter to NIL:

 

PROCEDURE MyProc(param1, param2, param3)
 IF param2 != NIL
         param2 := "default value"
 ENDIF
 <Statements>...

 

This example invokes the procedure, UpdateAmount(), as an aliased expression:

 

USE invoices NEW
USE customer NEW
Invoices->UpdateAmount(Amount + Amount * nInterest)

See Also

FIELD, FUNCTION, LOCAL, MEMVAR, METHOD,RETURN