Movatterモバイル変換


[0]ホーム

URL:


Skip to main content

This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Download Microsoft EdgeMore info about Internet Explorer and Microsoft Edge
Table of contentsExit editor mode

Interfaces (Visual Basic)

Feedback

In this article

Interfaces define the properties, methods, and events that classes can implement. Interfaces allow you to define features as small groups of closely related properties, methods, and events; this reduces compatibility problems because you can develop enhanced implementations for your interfaces without jeopardizing existing code. You can add new features at any time by developing additional interfaces and implementations.

There are several other reasons why you might want to use interfaces instead of class inheritance:

  • Interfaces are better suited to situations in which your applications require many possibly unrelated object types to provide certain functionality.

  • Interfaces are more flexible than base classes because you can define a single implementation that can implement multiple interfaces.

  • Interfaces are better in situations in which you do not have to inherit implementation from a base class.

  • Interfaces are useful when you cannot use class inheritance. For example, structures cannot inherit from classes, but they can implement interfaces.

Declaring Interfaces

Interface definitions are enclosed within theInterface andEnd Interface statements. Following theInterface statement, you can add an optionalInherits statement that lists one or more inherited interfaces. TheInherits statements must precede all other statements in the declaration except comments. The remaining statements in the interface definition should beEvent,Sub,Function,Property,Interface,Class,Structure, andEnum statements. Interfaces cannot contain any implementation code or statements associated with implementation code, such asEnd Sub orEnd Property.

In a namespace, interface statements areFriend by default, but they can also be explicitly declared asPublic orFriend. Interfaces defined within classes, modules, interfaces, and structures arePublic by default, but they can also be explicitly declared asPublic,Friend,Protected, orPrivate.

Note

TheShadows keyword can be applied to all interface members. TheOverloads keyword can be applied toSub,Function, andProperty statements declared in an interface definition. In addition,Property statements can have theDefault,ReadOnly, orWriteOnly modifiers. None of the other modifiers—Public,Private,Friend,Protected,Shared,Overrides,MustOverride, orOverridable—are allowed. For more information, seeDeclaration Contexts and Default Access Levels.

For example, the following code defines an interface with one function, one property, and one event.

Interface IAsset    Event ComittedChange(ByVal Success As Boolean)    Property Division() As String    Function GetID() As IntegerEnd Interface

Implementing Interfaces

The Visual Basic reserved wordImplements is used in two ways. TheImplements statement signifies that a class or structure implements an interface. TheImplements keyword signifies that a class member or structure member implements a specific interface member.

Implements Statement

If a class or structure implements one or more interfaces, it must include theImplements statement immediately after theClass orStructure statement. TheImplements statement requires a comma-separated list of interfaces to be implemented by a class. The class or structure must implement all interface members using theImplements keyword.

Implements Keyword

TheImplements keyword requires a comma-separated list of interface members to be implemented. Generally, only a single interface member is specified, but you can specify multiple members. The specification of an interface member consists of the interface name, which must be specified in an implements statement within the class; a period; and the name of the member function, property, or event to be implemented. The name of a member that implements an interface member can use any legal identifier, and it is not limited to theInterfaceName_MethodName convention used in earlier versions of Visual Basic.

For example, the following code shows how to declare a subroutine namedSub1 that implements a method of an interface:

Class Class1    Implements interfaceclass.interface2    Sub Sub1(ByVal i As Integer) Implements interfaceclass.interface2.Sub1    End SubEnd Class

The parameter types and return types of the implementing member must match the interface property or member declaration in the interface. The most common way to implement an element of an interface is with a member that has the same name as the interface, as shown in the previous example.

To declare the implementation of an interface method, you can use any attributes that are legal on instance method declarations, includingOverloads,Overrides,Overridable,Public,Private,Protected,Friend,Protected Friend,MustOverride,Default, andStatic. TheShared attribute is not legal since it defines a class rather than an instance method.

UsingImplements, you can also write a single method that implements multiple methods defined in an interface, as in the following example:

Class Class2    Implements I1, I2    Protected Sub M1() Implements I1.M1, I1.M2, I2.M3, I2.M4    End SubEnd Class

You can use a private member to implement an interface member. When a private member implements a member of an interface, that member becomes available by way of the interface even though it is not available directly on object variables for the class.

Interface Implementation Examples

Classes that implement an interface must implement all its properties, methods, and events.

The following example defines two interfaces. The second interface,Interface2, inheritsInterface1 and defines an additional property and method.

Interface Interface1    Sub sub1(ByVal i As Integer)End Interface' Demonstrates interface inheritance.Interface Interface2    Inherits Interface1    Sub M1(ByVal y As Integer)    ReadOnly Property Num() As IntegerEnd Interface

The next example implementsInterface1, the interface defined in the previous example:

Public Class ImplementationClass1    Implements Interface1    Sub Sub1(ByVal i As Integer) Implements Interface1.sub1        ' Insert code here to implement this method.    End SubEnd Class

The final example implementsInterface2, including a method inherited fromInterface1:

Public Class ImplementationClass2    Implements Interface2    Dim INum As Integer = 0    Sub sub1(ByVal i As Integer) Implements Interface2.sub1        ' Insert code here that implements this method.    End Sub    Sub M1(ByVal x As Integer) Implements Interface2.M1        ' Insert code here to implement this method.    End Sub    ReadOnly Property Num() As Integer Implements Interface2.Num        Get            Num = INum        End Get    End PropertyEnd Class

You can implement a readonly property with a readwrite property (that is, you do not have to declare it readonly in the implementing class). Implementing an interface promises to implement at least the members that the interface declares, but you can offer more functionality, such as allowing your property to be writable.

Related Topics

TitleDescription
Walkthrough: Creating and Implementing InterfacesProvides a detailed procedure that takes you through the process of defining and implementing your own interface.
Variance in Generic InterfacesDiscusses covariance and contravariance in generic interfaces and provides a list of variant generic interfaces in the .NET Framework.
Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, seeour contributor guide.

Feedback

Was this page helpful?

YesNoNo

Need help with this topic?

Want to try using Ask Learn to clarify or guide you through this topic?

Suggest a fix?

  • Last updated on

In this article

Was this page helpful?

YesNo
NoNeed help with this topic?

Want to try using Ask Learn to clarify or guide you through this topic?

Suggest a fix?