Condividi tramite


Public (Visual Basic)

Specifies that one or more declared programming elements have no access restrictions.

Remarks

If you are publishing a component or set of components, such as a class library, you usually want the programming elements to be accessible by any code that interoperates with your assembly. To confer such unlimited access on an element, you can declare it with Public.

Public access is the normal level for a programming element when you do not need to limit access to it. Note that the access level of an element declared within an interface, module, class, or structure defaults to Public if you do not declare it otherwise.

Rules

  • Declaration Context. You can use Public only at module, interface, or namespace level. This means the declaration context for a Public element must be a source file, namespace, interface, module, class, or structure, and cannot be a procedure.

Behavior

  • Access Level. All code that can access a module, class, or structure can access its Public elements.

  • Default Access. Local variables inside a procedure default to public access, and you cannot use any access modifiers on them.

  • Access Modifiers. The keywords that specify access level are called access modifiers. For a comparison of the access modifiers, see Access Levels in Visual Basic.

The Public modifier can be used in these contexts:

Class Statement

Const Statement

Declare Statement

Delegate Statement

Dim Statement

Enum Statement

Event Statement

Function Statement

Interface Statement

Module Statement

Operator Statement

Property Statement

Structure Statement

Sub Statement

See Also

Reference

Protected (Visual Basic)

Friend (Visual Basic)

Private (Visual Basic)

Concepts

Access Levels in Visual Basic

Procedures in Visual Basic

Other Resources

Structures (Visual Basic)

Objects and Classes in Visual Basic