Declaration Contexts and Default Access Levels (Visual Basic)

 

Updated: July 20, 2015

For the latest documentation on Visual Studio 2017 RC, see Visual Studio 2017 RC Documentation.

This topic describes which Visual Basic types can be declared within which other types, and what their access levels default to if not specified.

The declaration context of a programming element is the region of code in which it is declared. This is often another programming element, which is then called the containing element.

The levels for declaration contexts are the following:

  • Namespace level — within a source file or namespace but not within a class, structure, module, or interface

  • Module level — within a class, structure, module, or interface but not within a procedure or block

  • Procedure level — within a procedure or block (such as If or For)

The following table shows the default access levels for various declared programming elements, depending on their declaration contexts.

Declared elementNamespace levelModule levelProcedure level
Variable (Dim Statement)Not allowedPrivate (Public in Structure, not allowed in Interface)Public
Constant (Const Statement)Not allowedPrivate (Public in Structure, not allowed in Interface)Public
Enumeration (Enum Statement)FriendPublicNot allowed
Class (Class Statement)FriendPublicNot allowed
Structure (Structure Statement)FriendPublicNot allowed
Module (Module Statement)FriendNot allowedNot allowed
Interface (Interface Statement)FriendPublicNot allowed
Procedure (Function Statement, Sub Statement)Not allowedPublicNot allowed
External reference (Declare Statement)Not allowedPublic (not allowed in Interface)Not allowed
Operator (Operator Statement)Not allowedPublic (not allowed in Interface or Module)Not allowed
Property (Property Statement)Not allowedPublicNot allowed
Default property (Default)Not allowedPublic (not allowed in Module)Not allowed
Event (Event Statement)Not allowedPublicNot allowed
Delegate (Delegate Statement)FriendPublicNot allowed

For more information, see Access Levels in Visual Basic.

Friend
Private
Public

Show: