Working with Visual Basic Interfaces

What Are Visual Basic Interfaces?

Visual Basic Interfaces describe the characteristics of methods, properties, and events, but leave the implementation details up to structures or classes. Interfaces define methods, properties, and events that classes can implement.

Visual Basic Interfaces have the following advantages:

  • You can use Interfaces when you cannot use class inheritance.
  • With Visual Basic Interfaces, 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.

Differences Between Visual Basic Interfaces and Visual Basic Classes

There are a few differences between interfaces and classes. These are as follows:

  • Interfaces should be implemented in classes as members tagged with the implements clause. Class members need no special suffixes.
  • Classes are able to inherit from one class and implement multiple interfaces whereas interfaces provide no implementation.
  • Interface members are public. Class members can use any access specifier.
  • Interfaces support multiple interface inheritance. Classes support single inheritance only.

Our Visual Basic Interface Project

Start Visual Studio and create a new Windows Forms project. Design your form to resemble Figure 1.

Figure 1: Design

You will be creating two very small interfaces and implement each interface in a different class. Add a new class and name it Interface1. Replace the Class definition and add the following code:

Interface Interface1

   Sub Interface1_Sub_1()

End Interface

This creates an Interface named Interface1. Interface1 contains the signature for one Sub procedure. This sub procedure is named Interface1_Sub_1. Now, you need to make use of this interface. Add another class to your project and leave the default name (or, if you do rename it, please remember that when we refer to this class a bit later). Add the following code to your class:

Public Class Class1
   Implements Interface1

   Public Sub Class1_Sub_1() Implements _

      MessageBox.Show("Interface 1")

   End Sub

End Class

The Class implements Interface1 and so does the Class's sub procedure named Class1_Sub_1.

Repeat the preceding steps to add another interface named Interface2 and another class that implements Interface2. The code for both is shown next:

Interface Interface2

   Property Interface2_Property_1 As Integer

End Interface

Public Class Class2
   Implements Interface2

   Property Class2_Property_1 As Integer Implements _

End Class

This is the same principle as Interface1 and Class1, expect that Interface2 exposes a property instead of an ordinary sub. The property inside the second class should also still implement the Interface.

Add the following code behind the button labelled 'Interface1' on your Form:

   Private Sub Button1_Click(sender As Object, e As EventArgs) _
         Handles Button1.Click

      Dim c1 As New Class1


   End Sub

Once run, your program will produce a simple MessageBox. Add the following code behind the button labeled 'Interface 2':

   Private Sub Button2_Click(sender As Object, e As EventArgs) _
         Handles Button2.Click

      Dim c2 As New Class2

      c2.Class2_Property_1 = 25


   End Sub

Here, you have manipulated the Interface property inside Class2 to be 25 and shown inside the resulting MessageBox.

As mentioned earlier, you can add and implement Interface Events as well. Here is an example of such an Interface:

Public Interface Interface3

   Event Interface3_Event_1(ByVal Success As Boolean)

End Interface

And its Implementation:

Public Class Class3
   Implements Interface3

   Public Event Interface3_Event_1(Success As Boolean) Implements _

End Class

Please feel free to download the zip file named "Interface_Ex" for your use in completing this project.


Interfaces are extremely important in Object-Oriented Programming and using them productively can save you tons of work. I hope you have benefited from this article.

This article was originally published on August 16th, 2017

About the Author

Hannes DuPreez

Hannes du Preez is a self-taught developer who started learning to program in the days of QBasic. He has written several articles over the years detailing his programming quests and adventures. .NET is his second love, just after his wife and kid. He has always been an avid supporter of .NET since the beginning and is an expert in VB and C#. He was given the Microsoft Most Valuable Professional Award for .NET (2008–2017). He has worked as a moderator and an article reviewer on online forums and currently works as a C# developer and writes articles for CodeGuru.com, Developer.com, DevX.com, and the Database journal.
His first book Visual Studio 2019 In Depth is currently on sale on Amazon and Bpb Publications.

You could reach him at: ojdupreez1978[at]gmail[dot]com

Related Articles


Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date