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 _ Interface1.Interface1_Sub_1 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 _ Interface2.Interface2_Property_1 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 c1.Class1_Sub_1() 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 MessageBox.Show(c2.Class2_Property_1) 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 _ Interface3.Interface3_Event_1 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.