In TCE, you can define your own functions in a class.
These functions can have simple parameters or list parameters, as well as return a single value or list of values.
The simplest definition of a function is the following:
Public Function DoAny() As Void
End Function
If you create a function in the class editor called DoAny, you get this function core.
Basic samples of combinations of parameters and return types:
Public Function DoAny() As Void |
Simplest function definition No parameters and no return value |
Public Function DoAny() As Long |
No parameters and returns a long value DoAny is a local variable which can be used to define the return value |
Public Function DoAny() As Long() |
No parameters and return a list of longs DoAny is a local list variable which can be used to define the return values |
Public Function DoAny(ByVal Param1 As Long) As Void |
One Long parameter passed by value and no return value The caller parameter variable will not be changed |
Public Function DoAny(ByRef Param1 As Long) As Void |
One Long parameter passed by reference and no return value The caller parameter variable will be changed |
Public Function DoAny(ByVal Param1 As String, ByRef Param2 As Long) As Void |
One String parameter passed by value and one Long parameter passed by reference and no return value The caller parameter variable for Param2 will be changed |
Public Function DoAny(ByVal Param1 As String, Optional ByVal Param2 As Long := 1000) As Void |
One String parameter passed by value and one Long parameter optionally passed by value and no return value Param2 gets the default value of 1000 if not passed. |
Public Function DoAny() As Word.Application |
No parameters and returns a COM object reference DoAny is a local variable which can be used to define the return reference |
Public Function DoAny() As ::Cars |
No parameters and returns a TCE object reference DoAny is a local variable which can be used to define the return reference |
All parameters are separated by a comma. Parameters with a leading ByVal are passed as a value to the function, i.e. changes to the value of the parameter variable are not returned to the caller of the function. However, if you want to achieve this exactly, the parameter must be specified with the addition ByRef. Lists can also be passed to ByVal or ByRef.
Parameters can also be passed optionally.
Optional parameters do not need to be specified when the function is called.
Public Function DoAny(ByVal Param1 As Long, Optional ByVal Param2 As Long) As
Void
Return If IsMissing(Param2)
End Function
The transfer of a value via an optional parameter can be checked with the function IsMissing:
If you want to cancel the processing of a function prematurely, the command Exit Function [Expression] or alternatively Return [Expression] can be used.
Public Function DoAny(ByVal Param1 As Long) As Long
If Param1 = 10 Then
Exit Function 10
EndIf
DoAny := 100
End Function
Scope
Functions can have a limited scope. This is controlled by the keywords Public, Friend, or Private.
Scope |
Description |
Public |
The function can be accessed from all objects. This also applies to calls from link knowledge bases. |
Friend |
The function is only available within your own knowledge base. |
Private |
The function is available only within the class and from derived classes. |
Function Attributes
Functions can have additional attributes. The attributes are specified between the scope key term and the function name. Multiple attributes can be specified if necessary.
Function attributes |
Description |
Trigger |
The function can be used as a call target of object triggers |
Async |
The function can be called asynchronously. The return type of the function can only be Void or TCEServer.AsyncController. |
NotOverrideable |
The function must not be over-defined. |
Static |
The function is statically declared and can be executed without the underlying object. Static functions cannot access non static elements. This includes objects, object references, characteristics, and other, but not constants. A static function must be programmed with the passed parameters, local variables, and class constants. |
Deactivated |
The function is disabled and is treated as if it did not exist. |
Example of function attributes:
Public Static Function AddTwoNumbers(ByVal Num1 As Double, ByVal Num2 As Double) As Double
End Function Num1 + Num2