Inherits Statement - Visual Basic (original) (raw)

Causes the current class or interface to inherit the attributes, variables, properties, procedures, and events from another class or set of interfaces.

Syntax

Inherits basetypenames  

Parts

Term Definition
basetypenames Required. The name of the class from which this class derives. -or- The names of the interfaces from which this interface derives. Use commas to separate multiple names.

If used, the Inherits statement must be the first non-blank, non-comment line in a class or interface definition. It should immediately follow the Class or Interface statement.

You can use Inherits only in a class or interface. This means the declaration context for an inheritance cannot be a source file, namespace, structure, module, procedure, or block.

Rules

An example of class inheritance in the .NET Framework is the ArgumentException class, which inherits from the SystemException class. This provides to ArgumentException all the predefined properties and procedures required by system exceptions, such as the Message property and the ToString method.

An example of interface inheritance in the .NET Framework is the ICollection interface, which inherits from the IEnumerable interface. This causes ICollection to inherit the definition of the enumerator required to traverse a collection.

Example 1

The following example uses the Inherits statement to show how a class named thisClass can inherit all the members of a base class named anotherClass.

Public Class thisClass
    Inherits anotherClass
    ' Add code to override, overload, or extend members
    ' inherited from the base class.
    ' Add new variable, property, procedure, and event declarations.
End Class

Example 2

The following example shows inheritance of multiple interfaces.

Public Interface thisInterface
    Inherits IComparable, IDisposable, IFormattable
    ' Add new property, procedure, and event definitions.
End Interface

The interface named thisInterface now includes all the definitions in the IComparable, IDisposable, and IFormattable interfaces The inherited members provide respectively for type-specific comparison of two objects, releasing allocated resources, and expressing the value of an object as a String. A class that implements thisInterface must implement every member of every base interface.

See also