A visibility scope of a code element determines whether it can be used, that is, whether another code is allowed to read this element and record data to it. A visibility scope depends not only on the method of element declaration but also on the access level of an element container. To specify a visibility scope, use reserved words, that is, access modifiers. Below is given a comparison of access modifiers available in the Fore language.
Access modifier | Description | Elements that can be declared using this modifier | Declaration context where a modifier can be used |
Public | A modifier indicates whether an element is available anywhere in the code in the current unit, in all units of an assembly and in the units, for which the current unit is connected via a reference. | classes, interfaces, delegates, constants, variables, procedures and functions, properties, enumerations, events |
class, unit, namespace, interface |
Private | An access modifier indicates element availability only within its declaration context. This modifier is used to completely hide the details of internal implementation of various structures. | classes, interfaces, delegates, constants, variables, procedures and functions, properties, enumerations, events |
class, unit, namespace, interface |
Protected | An access modifier indicates element availability within a class and in all classes derived from it. | constants, variables, procedures and functions, properties, events |
class, interface |
Friend | An access modifier indicates whether the element is available anywhere in the code of the current unit and in all units of the current assembly. Elements with the Friend modifier are not available in external assemblies. |
constants, variables, procedures and functions, properties, events |
class, interface |
Protected Friend | Access modifiers used together indicate that the elements can be addressed from any unit of the current assembly, and from a derived class located in unit of another assembly. | constants, variables, procedures and functions, properties, events |
class, interface |
Executing the example requires the following:
Create a new assembly.
Create a new unit and a form in the assembly.
Locate the Button component named Button1 and the EditBox component named EditBox1 on the form. Create the OnClick event handler for the button.
Specify the following code in the unit and in the event handler:
Class TestObject: Object
Private s: String;
//Name property
Friend Property Name: String
Get
Begin
Return s;
End Get
Set
Begin
s := Value;
End Set
End Property Name;
//Check the set name
Friend Function TestName: Integer;
Begin
If s = "" Then
Return - 1
Else
Return TestStructure;
End If;
End Function TestName;
//Function for searching for duplicate characters in a name
Protected Function TestStructure: Integer;
Var
i: Integer;
Begin
For i := 65 To 122 Do
If s.IndexOf(Char.Chr(i) + Char.Chr(i)) <> -1 Then
Break;
End If;
End For;
If i <> 123 Then
Return 0;
Else
Return 1;
End If;
End Function TestStructure;
End Class TestObject;
Sub Button1OnClick(Sender: Object; Args: IMouseEventArgs);
Var
Obj: TestObject;
i: Integer;
Begin
Obj := New TestObject.Create;
Obj.Name := EditBox1.Text;
i := Obj.TestName;
If i = -1 Then
WinApplication.InformationBox("The name has not been specified");
Elseif i = 0 Then
WinApplication.InformationBox("The name contains duplicate characters");
Elseif i = 1 Then
WinApplication.InformationBox("The name is correct");
End If;
End Sub Button1OnClick;
After a form has been started, clicking the button creates a new object of the TestObject custom class. The Name property is defined for the object. This property is implemented in the TestObject class and can be read and recorded in any unit of the current assembly. The text entered in the EditBox1 component is set as the value of the Name property. The name value is stored in the "s" variable. The "s" variable is available only in the TestObject class.
After the value has been defined, the name is checked. To check the name, the TestName function is called, which is also available in any unit of the current assembly. If the set name is not empty, the additional TestStructure function is called from the TestName function. The TestStructure function checks the name for duplicate English characters. This function is available in the TestObject class as well as in all classes derived from it.
After the name has been checked, the appropriate message stating that it is correct appears.
See also: