Partager via


Shadows 

Specifies that a declared programming element redeclares and hides an identically named element, or set of overloaded elements, in a base class.

Remarks

The main purpose of shadowing (which is also known as hiding by name) is to preserve the definition of your class members. The base class might undergo a change that creates an element with the same name as one you have already defined. If this happens, the Shadows modifier forces references through your class to be resolved to the member you defined, instead of to the new base class element.

Both shadowing and overriding redefine an inherited element, but there are significant differences between the two approaches. For more information, see Shadowing in Visual Basic.

Rules

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

    You can declare only one shadowing element in a single declaration statement.

  • Combined Modifiers. You cannot specify Shadows together with Overloads, Overrides, or Static in the same declaration.

  • Element Types. You can shadow any kind of declared element with any other kind. If you shadow a property or procedure with another property or procedure, the parameters and the return type do not have to match those in the base class property or procedure.

  • Accessing. The shadowed element in the base class is normally unavailable from within the derived class that shadows it. However, the following considerations apply.

    • If the shadowing element is not accessible from the code referring to it, the reference is resolved to the shadowed element. For example, if a Private element shadows a base class element, code that does not have permission to access the Private element accesses the base class element instead.

    • If you shadow an element, you can still access the shadowed element through an object declared with the type of the base class. You can also access it through MyBase.

The Shadows 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

Property Statement

Structure Statement

Sub Statement

See Also

Reference

Shared (Visual Basic)
Static (Visual Basic)
Private (Visual Basic)
MyBase
MustOverride
NotOverridable
Overloads
Overridable
Overrides

Concepts

Shadowing in Visual Basic