Saturday, May 19, 2018

Visibility modifiers

In this post I try to explain the visibility modifiers available within Oxygene Pascal.

The visibility of a class is an OO way to hide the implementation of the class for other code.
The purpose of this is that other code can not influence the state of the object in a way that is unexpected within the design.

In VB.Net I had the following four visibility modifiers (ordered from most hidden to least hidden):

Private (VB.Net)Private means in VB.Net that the members and types are only visible within the class implementation itself and within the same code unit - so any implementation of any class within the same code module can see this code.
Friend (VB.Net)Friend means in VB.Net that them embers and types are visible to all code within the same project.
Protected (VB.Net)Protected means in VB.Net that the members and types are only visible within the class implementation itself, in all the classes that inherit from it and within the same code unit - so any implementation of any class within the same code module can see this code.
Public (VB.Net)Public means in VB.Net that the members and types are visible to all code, including other projects that references this assembly.
As you can see, in VB.Net is is impossible to hide code from other code within the same class.
Note: There were 2 more added recently; Protected Friend and Private Protected

With Oxygene, you can manage the visibility a lot better; instead of only four levels of visibility, Oxygene provides nine levels! As with a lot of things in the Oxygene language, the developers thought of everything possible (ordered from most hidden to least hidden):

Private
(Oxygene)
Private means that the members are only visible within the class implementation itself. And nowhere else. (also know as strict private).
You can only use this for members (fields, properties and methods), not for types.
Unit and Protected
(Oxygene)
Unit and Protected means in that the members and types are only visible within the class implementation itself, and to classes the inherit from this class as long as they are defined in the same code file.
You can only use this for members (fields, properties and methods), not for types.
Unit
(Oxygene)
Unit means that the members and types are only visible within the current code unit.
(same as Private in VB.Net)
Unit or Protected
(Oxygene)
Unit or Protected means that the members and types are only visible within the class implementation itself, to classes the inherit from this class and to all code within the same code file.
You can only use this for members (fields, properties and methods), not for types.
(same as Protected in VB.Net- but there it can be used for types also)
Assembly and Protected
(Oxygene)
Assembly and Protected means  that the members and types are only visible within the class implementation itself, and to classes the inherit from this class as long as they are defined in the same project.
You can only use this for members (fields, properties and methods), not for types.
(same as Private Protected in VB.Net)
Assembly
(Oxygene)
Assembly means that the members and types are only visible within the current project.
(same as Friend in VB.Net)
Assembly or Protected
(Oxygene)
Assembly or Protected means that the members and types are only visible within the class implementation itself, to classes the inherit from this class and to all code within the project.
You can only use this for members (fields, properties and methods), not for types.
(same as Protected Friend in VB.Net)
Protected
(Oxygene)
Protected means that the members and types are only visible within the class implementation itself and to classes the inherit from this class.
Public
(Oxygene)
Public means in that the members and types are visible to all code, including other projects that references this assembly.
(same as Public in VB.Net)

In Free Pascal and Delphi there is also the Published visibility modifier - that is used to publish properties for the form designers. This is not needed in .Net; the BrowsableAttribute is used for this.

To be exact, the Public modifier in Oxygene is analog to the published modifier in Free Pascal and Delphi. To get the Public behavior from Free Pascal and Delphi, you need to use the Browsable attribute:

public
  [Browsable(False)]
  property ....

And that was all folks.
In the next post I will talk about the method definition.

No comments:

Post a Comment