Interfaces

It is useful to make a distinction between a class’s interface and its implementation. Conceptually, the interface of a class is the set of members that are visible to users of the class—i.e., the class’s public members. The public members are thought of as comprising the class’s interface because they are the only way that code outside of the class can interact (i.e., interface) with objects of that class. In contrast, the implementation is comprised of the class’s code plus the set of members that are not public.

It is possible to take this interface concept further and separate interface definition from class definition altogether. This has benefits that will be shown shortly. To define an interface, use the Interface statement:

Public Interface ISomeInterface
   Sub SomeSub(  )
   Function SomeFunction(  ) As Integer
   Property SomeProperty(  ) As String
   Event SomeEvent( _
      ByVal sender As Object, _
      ByVal e As SomeEventArgs _
   )
End Interface

An interface declaration defines methods, properties, and events that will ultimately be implemented by some class or structure definition. Because interfaces never include any implementation, the declarations are headers only—never any implementation code; End Sub, End Function, or End Property statements; or property get or set blocks. There are no access modifiers (Public, Private, etc.) because all members of an interface are public by definition. By convention, interface names start with the letter “I”.

To provide an implementation ...

Get Programming Visual Basic .NET now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.