Application Security Testing: An Integral Part of DevOps
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.
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 AClass End Class Public Interface AnInterface End 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 String End 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 Function End 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 interface's 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 firstname.lastname@example.org for help building VB.NET applications or migrating VB6 applications to .NET.