Níveis de acesso em Visual Basic
O nível de acesso de um elemento declarado é a extensão da habilidade de acessá-lo, ou seja, o que o código tem permissão para ler ou gravar nela. This is determined not only by how you declare the element itself, but also by the access level of the element's container. Code that cannot access a containing element cannot access any of its contained elements, even those declared as Public. For example, a Public variable in a Private structure can be accessed from inside the class that contains the structure, but not from outside that class.
Public
The Público (Visual Basic) keyword in the declaration statement specifies that the elements can be accessed from code anywhere in the same project, from other projects that reference the project, and from any assembly built from the project. The following code shows a sample Public declaration.
Public Class classForEverybody
You can use Public only at module, interface, or namespace level. This means you can declare a public element at the level of a source file or namespace, or inside an interface, module, class, or structure, but not in a procedure.
Protected
The Protegido (Visual Basic) keyword in the declaration statement specifies that the elements can be accessed only from within the same class, or from a class derived from this class. The following code shows a sample Protected declaration.
Protected Class classForMyHeirs
You can use Protected only at class level, and only when you declare a member of a class. This means you can declare a protected element in a class, but not at the level of a source file or namespace, or inside an interface, module, structure, or procedure.
Friend
The Friend (Visual Basic) keyword in the declaration statement specifies that the elements can be accessed from within the same assembly, but not from outside the assembly. The following code shows a sample Friend declaration.
Friend stringForThisProject As String
You can use Friend only at module, interface, or namespace level. This means you can declare a friend element at the level of a source file or namespace, or inside an interface, module, class, or structure, but not in a procedure.
Protected Friend
The Protected and Friend keywords together in the declaration statement specify that the elements can be accessed either from derived classes or from within the same assembly, or both. O código a seguir mostra um exemplo Protected Frienddedeclaração.
Protected Friend stringForProjectAndHeirs As String
Você pode usar Protected Friend somente à classe nível e somente quando você declara um membro de uma classe. This means you can declare a protected friend element in a class, but not at the level of a source file or namespace, or inside an interface, module, structure, or procedure.
Private
The Private (Visual Basic) keyword in the declaration statement specifies that the elements can be accessed only from within the same module, class, or structure. The following code shows a sample Private declaration.
Private numberForMeOnly As Integer
Você pode usar Private somente no nível de módulo . This means you can declare a private element inside a module, class, or structure, but not at the level of a source file or namespace, inside an interface, or in a procedure.
At the module level, the Dim statement without any access level keywords is equivalent to a Private declaration. However, you might want to use the Private keyword to make your code easier to read and interpret.
Access Modifiers
The keywords that specify access level are called access modifiers. The following table compares the access modifiers.
Access modifier |
Access level granted |
Elements you can declare with this access level |
Declaration context within which you can use this modifier |
---|---|---|---|
Public |
Irrestrito: Any code that can see a public element can access it |
Interfaces Modules Classes Structures Structure members Procedures Properties Member variables Constants Enumerations Events External declarations Delegates |
Source file Namespace Interface Module Class Structure |
Protected |
Derivational: Code in the class that declares a protected element, or a class derived from it, can access the element |
Interfaces Classes Structures Procedures Properties Member variables Constants Enumerations Events External declarations Delegates |
Class |
Friend |
Assembly: Code in the assembly that declares a friend element can access it |
Interfaces Modules Classes Structures Structure members Procedures Properties Member variables Constants Enumerations Events External declarations Delegates |
Source file Namespace Interface Module Class Structure |
Protected Friend |
Union of Protected and Friend: Code in the same class or the same assembly as a protected friend element, or within any class derived from the element's class, can access it |
Interfaces Classes Structures Procedures Properties Member variables Constants Enumerations Events External declarations Delegates |
Class |
Private |
Declaration context: Code in the type that declares a private element, including code within contained types, can access the element |
Interfaces Classes Structures Structure members Procedures Properties Member variables Constants Enumerations Events External declarations Delegates |
Module Class Structure |
Consulte também
Tarefas
Como: Controlar a disponibilidade de uma variável (Visual Basic)
Referência
Conceitos
Nomes de elementos declarados (Visual Basic)
Características do elemento declarado (Visual Basic)
Declaração de variável no Visual Basic