Methods

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.

Example

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 <> 0And (o <> NullThen
            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.

Example

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.

Example

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.

Example

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:

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.

Method Parameters

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:

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(1Var Result);
    Obj.B(Out Result);
    Arr[0] := 1;
    Arr[1] := 2;
    Arr[2] := 3;
    Obj.C(Arr);
End Sub;

Method Body

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:

Classes