Method is a class member that implements certain calculations or actions with an object or class. Methods are procedures (they execute some calculations and do no return result) and functions (they execute some calculations and return result).
There are the following types of methods in Fore.NET:
When a method declaration includes the Shared modifier, this method is named a static method. When no Shared modifier is present, the method is named an instance method.
A static method does not operate on a specific instance, and it is a compilation error to use the Self structure in a static method.
An instance method operates on a given instance of a class, and that instance can be obtained using the Self structure.
If a method signature contains the Override modifier, it means that this method overrides implementation of an inherited virtual method of the basic class.
Class SharedMethod
Public Shared i: integer;
Public Shared o: object;
Shared b: boolean;
Public Shared Sub Reset();
Begin
i := 0;
o := Null;
End Sub;
Public Shared Function TestObject(): boolean;
Begin
If (i <> 0) And (o <> Null) Then
b := True;
Else
b := False;
End If;
Return b;
End Function;
Public Sub Execute();
Begin
End Sub Execute;
End Class;
Sub Test();
Var
Obj: SharedMethod = New SharedMethod();
Begin
SharedMethod.Reset();
...
SharedMethod.i := //Set value
SharedMethod.o := //Set value
...
If SharedMethod.TestObject() Then
//Operations if function returns True
Else
//Operations if function returns False
End If;
Obj.Execute();
End Sub;
If a method signature contains the Virtual modifier, this method is named a virtual method. Otherwise, the method is non-virtual.
The implementation of a non-virtual method is invariant relative to the type of object, for which the method is invoked. Implementation of a virtual method can be changed in derived classes. The process of changing the implementation of an inherited virtual method in a derived class is known as overriding of that method.
In a virtual method invocation, the real type of object, for which that invocation takes place, is declared and the virtual method implementation is selected during the program runtime. In non-virtual method invocation, the compile-time type of the object is declared.
If implementation of the virtual method contains calling of Inherited, the control is passed to the previous implementation of this method in one of parent classes. If the control passes to the virtual series by calling Inherited, the called method is declared relative to the class, in which implementation of the method containing the Inherited call is declared.
Class A
Public Virtual Sub Test();
Begin
End Sub;
Public Sub Test1();
Begin
End Sub Test1;
End Class;
Class B: A
Public Override Sub Test();
Begin
End Sub;
Public Sub Run();
Begin
Inherited Test();
End Sub;
End Class;
Class C: B
Public Override Sub Test();
Begin
End Sub;
New Public Sub Test1();
Begin
End Sub Test1;
End Class;
Sub Main();
Var
p: C = New C();
Begin
p.Run(); // Call the B.Run method, which passes control to the A.Test method
(p As A).Test(); // Call of the last implementation of the Test - C.Test method
(p As A).Test1(); // A.Test1 method call
End Sub;
If signature of a class instance method contains the Final modifier, this method is named final. Declaration of a final method must also include the Override modifier. The use of the Final modifier prevents a derived class from further overriding the method.
Class A
Public Virtual Sub Test();
Begin
End Sub;
End Class;
Class B: A
Public Final Override Sub Test();
Begin
End Sub;
End Class;
Class C: B
//Compilation error occurs when describing this method
//because method is final in source class B
Public Override Sub Test();
Begin
End Sub;
End Class C;
If the signature of an instance method contains the Abstract modifier, this method is named abstract. Abstract methods are implicitly virtual, so the Virtual modifier cannot be used when declaring an abstract method.
An abstract method declaration introduces a new virtual method but does not describe implementation of this method. Abstract method declarations are only permitted in abstract classes. All not-abstract classes that derive from an abstract one are to provide for implementation of all abstract methods by redetermining them.
Using an abstract method in a basic class access causes a compilation error.
An abstract method declaration is permitted to override a virtual method.
Abstract Class AbstractMethod
Abstract Public Sub Test();
Abstract Protected Function Run(): boolean;
End Class;
Class Sample: AbstractMethod
b: boolean;
Public Override Sub Test();
Begin
End Sub;
Protected Override Function Run(): boolean;
Begin
Return b;
End Function;
End Class;
The method is correct if all the following conditions are satisfied:
The declaration includes a valid combination of access modifiers.
Each modifier occurs only once.
The declaration includes at most one of the following modifiers: Shared, Virtual and Override.
The declaration includes at most one of the following modifiers: New and Override.
If the declaration includes the Abstract modifier, it should not include the following modifiers: Shared, Virtual and Final.
If the declaration includes the Private modifier, it should not include the following modifiers: Virtual, Override and Abstract.
If the declaration includes the Final modifier, it should also include the Override modifier.
When abstract methods are declared, only the method header is specified. The declaration of other methods includes the method body that contains operators executed when this method is invoked.
The name and formal parameters list of a method determine its signature.
The method signature may include a list of formal parameters, the last parameter may be an array parameter.
There are four kinds of formal parameters:
Value parameters. Value parameters are declared without any modifiers. A value parameter corresponds to a local variable that gets its initial value from the corresponding argument passed when the method is invoked. The argument passed as the value of a value parameter should be of a type that can be implicitly converted to the parameter type. New values can be assigned to a value parameter within the method body. Such assignments are local and have no effect on the actual argument given in the method invocation.
Reference parameters. A parameter declared with the Var modifier is a reference parameter. A reference parameter does not create a new storage location. Instead, it provides a memory space allocated to store the variable passed as an argument when invoking the method. An argument in a method invocation must be a variable of the same type as the parameter type.
Output parameters. A parameter declared with the Out modifier is an output parameter. Similar to reference parameters, an output parameter does not create a new storage location. Instead, it provides a memory space allocated to store the variable passed as an argument when invoking the method. The argument given as the output parameter value must be a variable of the same type as the parameter type. The output parameter value is initially undetermined. It needs to be determined before it is returned from the function.
Parameter-arrays. A parameter declared with the ParamArray modifier is a parameter array and permits declaring a method with a variable number of parameters. A parameter of this type must be the last parameter in the list and it must be of a one-dimensional array type. One of the following ways can be used to give arguments to a method with a variable number of parameters:
The argument given for a parameter array can be a value of the expression of a type that is implicitly convertible to the parameter array type.
Alternatively, the invocation can specify zero or more arguments for the parameter array where each argument is of a type that is implicitly convertible to the element type of the parameter array. In this case, the invocation creates an array instance containing the given argument values and uses it as the parameter value.
Except for allowing a variable number of arguments in an invocation, a parameter array is precisely equivalent to a value parameter of the same type.
Class MethodParams
//Parameter i is passed by value
//Parameter j is passed by reference
Public Sub A(i: integer; Var j: integer);
Begin
End Sub;
//Output parameter i
Public Sub B(Out i: integer);
Var
Result: integer;
Begin
//Procedure code
i := Result;
End Sub;
//Parameter-array Arr
Public Sub C(Paramarray Arr: array Of double);
Begin
End Sub;
End Class;
Sub Test();
Var
Obj: MethodParams = New MethodParams();
Result: integer;
Arr: array Of double = New double[3];
Begin
Obj.A(1, Var Result);
Obj.B(Out Result);
Arr[0] := 1;
Arr[1] := 2;
Arr[2] := 3;
Obj.C(Arr);
End Sub;
The method body consists of the operators block. Abstract methods do not have an implementation, and accordingly, the declaration of such methods does not include a body.
If a method does not return a value, it is not possible to use the Return statement in its body and specify the returned value. If the return type of a method is not void, each return statement in that method's body must specify an expression of a type that is implicitly convertible to the return type.
See also: