Using the Implements keyword
In this article, we will look at how we can use interfaces in VB using the Implements keyword.
Author: Jens G. BalchenBefore you startDefining InterfacesSo, how do we use interfaces? We start by defining the interface. This is done in a separate class: ' This is the code for the class TeenageGirl, which defines an interface. Option Explicit Public Property Let Name (ByVal n As String) End Property Public Property Get Name () As String End Property Public Property Let Age (ByVal a As Integer) End Property Public Property Get Age () As Integer End Property Public Sub GoShopping () End Sub Public Sub GoToSleep () End Sub Public Sub CallFriend (girlfriend As TeenageGirl) End SubAs you may already have noticed, there is no actual code here. All we have done is define the properties and methods that we feel a TeenageGirl object should have. In order to use this definition, we must decide to create our own TeenageGirl, but one that actually does something. Creating ValleyGirl' This is the code for our class ValleyGirl, which implements the ' TeenageGirl interface. This means that a ValleyGirl can do all ' the things that a TeenageGirl can do, and can act as a TeenageGirl ' anywhere she goes. Option Explicit Implements TeenageGirlHuh. That's what you call code? I chose to stop here to point you to a nice feature of the VB editor. If you look at the left-most combobox in the class window, you will find the usual (General) and Class sections, but you will also find TeenageGirl. If you choose TeenageGirl, the right-most combobox will be filled with all the properties and methods that the TeenageGirl interface contains. If you choose the CallFriend method, VB will automatically create code for you, like it does when you click Form_Load or cmdOK_Click. So let's fill in some code: Private Sub TeenageGirl_CallFriend(girlfriend As TeenageGirl) ' Let's call a friend. ' We can't actually place code here if we haven't got a phone ' object, but if you want to, you can write this code yourself. MsgBox "Cher is calling " & girlfriend.Name End SubAs you can see, we have implemented the method CallFriend. Now fill in the rest of the code: Private Property Let TeenageGirl_Age(ByVal RHS As Integer) ' We don't bother storing the age -- ValleyGirls ' are _always_ 16. End Property Private Property Get TeenageGirl_Age() As Integer TeenageGirl_Age = 16 End Property Private Sub TeenageGirl_GoShopping() ' Like, cool. MsgBox "Cher is going shopping." End Sub Private Sub TeenageGirl_GoToSleep() ' Snooze. MsgBox "Cher is going to sleep." End Sub Private Property Let TeenageGirl_Name(ByVal RHS As String) ' We don't bother storing this, since ValleyGirls are ' _always_ named Cher. End Property Private Property Get TeenageGirl_Name() As String TeenageGirl_Name = "Cher" End Property Let's create some code that can use these classes. In a module, create this code: Sub CallGoToSleep(girl As TeenageGirl) girl.GoToSleep() End Sub Sub Main() Dim girlA As New ValleyGirl Dim girlB As New ValleyGirl CallGoToSleep girlA CallGoToSleep girlB End SubSo CallGoToSleep expects a TeenageGirl object, and we give it a ValleyGirl object? It actually works, because ValleyGirl has the exact same interface as a TeenageGirl, and can act as a TeenageGirl. So far, we've only used one type of TeenageGirl; the ValleyGirl. There is really no use in using interfaces unless you plan to create more than one type of object, so let's create another girl; CityGirl. Creating CityGirl' This is the code for our class CityGirl, which implements the ' TeenageGirl interface. ' ' We modify the code for ValleyGirl a little bit. ' CityGirl is older than ValleyGirl, she's named Susan, and she ' refuses to call friends. Option Explicit Implements TeenageGirl Private Property Let TeenageGirl_Age(ByVal RHS As Integer) ' We don't bother storing the age -- CityGirls ' are _always_ 19. End Property Private Property Get TeenageGirl_Age() As Integer TeenageGirl_Age = 19 End Property Private Sub TeenageGirl_CallFriend(girlfriend As TeenageGirl) ' Refuse to call a friend. MsgBox "I refuse to call a friend." End Sub Private Sub TeenageGirl_GoShopping() ' Like, cool. MsgBox "Susan is going shopping." End Sub Private Sub TeenageGirl_GoToSleep() ' Snooze. MsgBox "Susan is going to sleep." End Sub Private Property Let TeenageGirl_Name(ByVal RHS As String) ' We don't bother storing this, since CityGirls are ' _always_ named Susan. End Property Private Property Get TeenageGirl_Name() As String TeenageGirl_Name = "Susan" End PropertyNow that we have a ValleyGirl and a CityGirl, let's try treating them both as TeenageGirls. Modify the code module: Sub CallGoToSleep(girl As TeenageGirl) girl.GoToSleep() End Sub Sub Main() Dim girlA As New CityGirl Dim girlB As New ValleyGirl CallGoToSleep girlA CallGoToSleep girlB End SubKinda neat. It works. Let's try something else: Sub CallFriend(girlA As TeenageGirl, girlB As TeenageGirl) girlA.CallFriend girlB End Sub Sub Main() Dim girlA As New CityGirl Dim girlB As New ValleyGirl CallFriend girlA, girlB CallFriend girlB, girlA End SubSo Susan (girlA) refuses to call Cher (girlB), because CityGirls don't call friends. Cher, on the other hand, calls Susan. The magic is complete -- our two classes ValleyGirl and CityGirl are both able to pose as TeenageGirls because they implement the correct interface. |
||
Editor: Jens G. Balchen Last update: 2025-01-02 Copyright 1995-2025 VBI |