Implementing Interfaces in VB.NET

What difference a year or two can bring. Two and a half years ago I thought I'd be writing a book titled Visual Basic 7 Unleashed for Sams. Completing the final drafts of Sams Visual Basic .NET Unleashed reminds me that a lot has changed in Visual Basic.

To help you make the transition as thoroughly and as completely as possible myself and other authors and contributors are writing a lot about threading, reflection, assemblies, COM interop, and delegates. But, reviewing programming subjects with a friend recently, I was reminded that there are developers at all levels, not just the advanced level. To be as thorough as possible, then, I am exploring advanced topics as well as non-advanced topics. (I welcome queries from readers too, and sometimes write an article based on several queries.)

This article is to the point. In this article we will examine interfaces: how to define them and how to implement them.

The idea of an interface is not new to COM, but in Visual Basic 6 every class was a COM interface. In Visual Basic .NET every class is a class and an interface is an interface, but not a COM interface. Classes have existed for decades and interfaces are not the sole purview of COM. A class describes the fields, properties, events, and methods a type will have. An interface is more like a portal or an attachment. An interface says that a type will implement these specific methods, but does not define what a type is. For example, a house can have a Window but so can a car. A type that implements a method open could be defined as something that has a Window, but the types could define cars, boats, houses, or the soul.

Visual Basic 6 does not support inheritance or classes in the object-oriented sense of the construct. VB6 does support COM interfaces. VB .NET, on the other supports classes and interfaces, so a distinguishment had to be made between the two idioms.

Defining Classes and Interfaces in VB .NET

The class and interface idioms use a very similar syntax when you define them. The following example defines an empty class in VB .NET, followed by an empty interface.

Public Class AClass

End Class

Public Interface AnInterface

End Interface

Classes can contain fields, properties, events, and methods. These elements of a class, called members, can have modifiers indicating that they are public, private, protected, or friend. All members of an interface declaration are public and as a result do not need nor can they have access modifiers.

Classes contain code; interfaces do not. However, classes that implement an interface do contain code. Keep in mind that there are no instances of interfaces in VB .NET. Every instance is a type that implements an interface, but is itself not an instance of the interface. (From this point we will leave the discussion of classes for another time and focus only on interfaces.)

Implementing Interfaces

Assuming we have an interface named AnInterface, we can only add method declarations to that interface. Extending the interface from the previous section, we can add a method named WhoAmI. The result is shown next.

Public Interface AnInterface
  Function WhoAmI() As String
End Interface

All types that implement the AnInterface interface must implement every declared method in that interface. In this example we only need to implement the function WhoAmI. Suppose AClass implements AnInterface; we would need to implement WhoAmI. The result of implement AnInterface in AClass would yield the following code.

Public Class AClass
  Implements AnInterface

  Public Function WhoAmI() As String Implements AnInterface.WhoAmI
    Return "AClass"
  End Function

End Class

The first thing we have to do is indicate that we want to implement the interface by name. Implements AnInterface tells consumers that AClass will implement all of the methods described in AnInterface. (The Visual Studio .NET IDE reminds us that we have to do so too.)

The difference between VB6 and VB .NET is that we have to add the Implements clause to the function body as shown in the listing. The function is declared as normal, but the clause Implements AnInterface.WhoAmI completes the contract between the class and the interface.

Final Thoughts

Structures can implement interfaces in VB.NET too. Whether a class or a structure is implementing an interface, you will need the Implements statement as demonstrated, and you will need to implement every method defined in the interface using the Implements clause at the end of the procedure header to indicate that a particular method satisfies a particular interface method.

Interfaces can be very short or very long. Methods described by an interface can be subroutines or functions, and they can be as elaborate or as simple as you need them to be. One method can implement more than one interface's method. Finally, keep in mind that interface methods can be called with a reference to the object or with a reference to the interface.

About the Author

Paul Kimmel is a freelance writer for and Look for cool Visual Basic .Net topics in his upcoming book Visual Basic .Net Unleashed available in January of 2002. Paul founded Software Conceptions, Inc. in 1990. Contact Paul Kimmel at for help building VB.NET applications or migrating VB6 applications to .NET.


  • Buying a new laptop... help?

    Posted by Matt Anderson on 07/16/2012 01:32pm

    Thanks for posting this. I just bought a new laptop that I am going to use VBNET on. I am a little worried about it shipping though... I think it might get broken. I ordered corrugated shipping boxes with it, so hopefully that will help... Any thoughts?

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • Live Event Date: October 29, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT It's well understood how critical version control is for code. However, its importance to DevOps isn't always recognized. The 2014 DevOps Survey of Practice shows that one of the key predictors of DevOps success is putting all production environment artifacts into version control. In this eSeminar, Gene Kim will discuss these survey findings and will share woeful tales of artifact management gone wrong! Gene will also share examples of how …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds