What are interfaces?
With Visual Basic 5.0, a new keyword was introduced to Visual Basic: Implements. The Implements keyword defines a new reality for VB programmers -- a world using interfaces.
Author: Jens G. Balchen

Before you start

You must know your way around classes and objects.

The Problem

Imagine an object. It has a set of public properties and methods that you can call. These properties and methods are referred to as an object's interface, because they define how you interact with the object. In order to use an object, you must know its interface -- if you didn't know, you wouldn't know what the methods were named or what parameters they take. Documenting an interface is a very important part of creating an object.

Now imagine that you have two objects. They do different things, yet are so similar that you want them to have the same interface. You create the two objects, and define their properties and methods similarly, giving them the same names and the same parameters. In theory, you should be able to use one or the other, and not know which one you're using. After all, they have the exact same interface.

In Visual Basic prior to version 5.0, this was done in the following manner:


' We have defined classes ClassA and ClassB, which both have the following public
' members:
'
' Property Get/Let Name
' Property Get/Let Age
' Sub GoShopping()
' Sub GoToSleep()
'
' What we want to do here, is to call GoToSleep() in an object that is either
' ClassA or ClassB.

Sub CallGoToSleep(obj As Object)
  
   obj.GoToSleep()
  
End Sub

' Somewhere else, we do this:

Sub Main()

Dim objA As New ClassA
Dim objB As New ClassB

   CallGoToSleep objA
   CallGoToSleet objB

End Sub

This approach will work, but it has some disadvantages.

The Disadvantages

Allthough ClassA and ClassB have the same interface, they are not the same class, hence objA and objB are not the same type of object. In CallGoToSleep, we must declare the parameter obj As Object. This means that we refer to it as a generic object.

What it really means is that we leave VB with no clue to what type of object this will be, giving us the freedom to use either objA or objB, but also robbing VB of the ability to perform tasks as auto completing property and method names, and checking that the object we pass to CallGoToSleep is of the correct type.


' In this sample, we will try to crash CallGoToSleep. We pass it an object of type 
' ClassC, and ClassC does not have the GoToSleep() method.

Sub CallGoToSleep(obj As Object)
  
   obj.GoToSleep()
  
End Sub

Sub Main()

Dim objC As New ClassC

   CallGoToSleep objC
	
End Sub
	
This time, you will get an error "438: Object doesn't support this property or method". As you may have noticed, VB didn't give you a warning before it started the program. That's because VB didn't know what kind of object you wanted to use in CallGoToSleep().

A better approach for CallGoToSleep() would be to define obj as a specific class. But that forces us to choose between either ClassA or ClassB, since an object is limited to only one type. Is there any way to work around this?

The Solution

The solution is interfaces. What we have here is a demand for two separate classes to share an interface. This can be done in VB using the Implements keyword. When an object implements an interface, it means it guarantees that it has all the properties and methods that are defined in the interface definition. It also means that VB will treat the object as having the type of the interface, so two objects, both implementing the same interface, could be treated as if they were the same type.

This may sound a little confusing, but what it really means is that in our example, we could treat both objA and objB as the same type, hereby removing the need to declare obj As Object. As a bonus, VB would be able to check the objects, and would refuse to accept objC if it didn't have the same type as objA and objB. In other words, you would get a warning before you started your program instead of having it crash at runtime.

Next section

In the next section, we will discuss how we can solve this problem in VB using the Implements keyword.