How to: Create and Implement Interfaces

As explained in Interfaces Overview, interfaces describe the properties, methods, and events of a class without providing any implementation.

To create an interface

  1. Define your interface by adding code to it that begins with the Interface keyword and the name of the interface, and ends with the End Interface statement. For example, the following code defines an interface named IAsset.

    Interface IAsset
    End Interface
    
  2. Add statements that define the properties, methods, and events your interface supports. For example, the following code defines one function, one property, and one event.

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

To implement an interface

  1. If the interface that you are implementing is not part of your project, add a reference to the assembly containing the interface.

  2. Create a new class that implements your interface, and include the Implements keyword in the line following the class name. For example, to implement the IAsset interface, you could name the implementation class Computer, as in the following code.

    Class Computer
        Implements IAsset
    End Class
    
  3. Add procedures to implement the properties, methods, and events of the class as in the following code, which builds on the example in the previous step:

    Class Computer
        Implements IAsset
    
        Public Event ComittedChange(ByVal Success As Boolean) _
           Implements IAsset.ComittedChange
    
        Private divisionValue As String 
    
        Public Property Division() As String _
            Implements IAsset.Division
    
            Get 
                Return divisionValue
            End Get 
            Set(ByVal value As String)
                divisionValue = value
                RaiseEvent ComittedChange(True)
            End Set 
        End Property 
    
        Private IDValue As Integer 
    
        Public Function GetID() As Integer _
            Implements IAsset.GetID
    
            Return IDValue
        End Function 
    
        Public Sub New(ByVal Division As String, ByVal ID As Integer)
            Me.divisionValue = Division
            Me.IDValue = ID
        End Sub 
    End Class
    

See Also

Tasks

Walkthrough: Creating and Implementing Interfaces

Concepts

Interfaces Overview

Interface Definition

Implements Keyword and Implements Statement

Interface Implementation Examples in Visual Basic

When to Use Interfaces

Reference

Interface Statement (Visual Basic)

Declaration Contexts and Default Access Levels

Other Resources

Inheritance in Visual Basic