This documentation is archived and is not being maintained.

Accessibility

The accessibility of a declared element is the ability to use it, that is, the permission for code to read it or write to it. This is determined not only by how you declare the element itself, but also by the accessibility of the element's container. If the containing element is not accessible, none of its contained elements are accessible, even those declared as Public. For example, a Public variable in a Private structure is accessible from inside the class containing the structure, but not from outside that class.

Public

The Public keyword in the Dim statement declares elements to be accessible from anywhere within the same project, from other projects that reference the project, and from an assembly built from the project. The following code shows a sample Public declaration:

Public Class ClassForEverybody 

You can use Public only at module, namespace, or file level. This means you can declare a public element in a source file or inside a module, class, or structure, but not within a procedure.

Protected

The Protected keyword in the Dim statement declares elements to be accessible 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 declaring a member of a class.

Friend

The Friend keyword in the Dim statement declares elements to be accessible from within the same project, but not from outside the project. The following code shows a sample Friend declaration:

Friend StringForThisProject As String 

You can use Friend only at module, namespace, or file level. This means you can declare a friend element in a source file or inside a module, class, or structure, but not within a procedure.

Protected Friend

The Protected and Friend keywords together in the Dim statement declare elements to be accessible either from derived classes or from within the same project, or both. The following code shows a sample Protected Friend declaration:

Protected Friend StringForProjectAndHeirs As String 

You can use Protected Friend only at class level, and only when declaring a member of a class.

Private

The Private keyword in the Dim statement declares elements to be accessible only from within the same module, class, or structure. The following code shows a sample Private declaration:

Private NumberForMeOnly As Integer 

You can use Private only at module, namespace, or file level. This means you can declare a private element in a source file or inside a module, class, or structure, but not within a procedure.

Note   At the module level, the Dim statement without any accessibility 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.

See Also

Declared Element Names | Declared Element Reference | Declared Element Characteristics | Lifetime | Scope | Variables | Variable Declaration | Dim Statement | Public | Protected | Friend | Private | Static

Show: