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 AClassEnd ClassPublic Interface AnInterfaceEnd 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.)
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 StringEnd 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 FunctionEnd 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.
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 interfaces 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 Developer.com and CodeGuru.com. 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 [email protected] for help building VB.NET applications or migrating VB6 applications to .NET.
# # #