During program development I accidentally noticed that all types that are declared within a classes has global visibility.
I have always thought that their visibility is restricted to class unless type is referred with class type name like TMyClass.TMytype.Value;
Am I doing something really wrong here, as structures like following:
unit MyTest;
interface
type TMyTest = class
constructor Create;
strict private
type TMyType = ( NUL, SLEEP );
end;
implementation
// ...
causes conflicts in other units that uses this (MyTest) unit.
If unit has Sleep( 100 ); call, it will conflict with TMyTest.TMyType.SLEEP and prevention of conflicts was why I encapsulated SLEEP inside class and TMyType at the first place.
Any suggestion for a workarounds?
Private members are not visible from outside the class. The default visibility allows access by other classes in the package. The protected modifier allows special access permissions for subclasses.
Private in Java means the variable or method is only accessible within the class where it is declared.
A public method can be invoked from anywhere—there are no restrictions on its use. A private method is internal to the implementation of a class, and it can only be called by other instance methods of the class (or, as we'll see later, its subclasses).
Public members can be accessed from the child class of the same package. Private members cannot be accessed from the child class of the same package. Public member can be accessed from non-child class of same package. Private members cannot be accessed from non-child class of same package.
This is actually by design. Your enum values have unit or global scope. They are not private since they are not part of the class. They are scoped at global level.
You can arrange for the enum values to have local scope by including the scoped enums directive:
{$SCOPEDENUMS ON}
The
$SCOPEDENUMS
directive enables or disables the use of scoped enumerations in Delphi code. More specifically,$SCOPEDENUMS
affects only definitions of new enumerations, and only controls the addition of the enumeration's value symbols to the global scope.In the
{$SCOPEDENUMS ON}
state, enumerations are scoped, and enum values are not added to the global scope. To specify a member of a scoped enum, you must include the type of the enum.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With