October 23rd, 2007
One problem some programmers new to VB.NET and object oriented programming in general have is understanding interfaces and how they related to classes. In this article, we’ll take an introductory look at interfaces and some of the ways you can use them in your VB.NET applications.
What is an Interface?
To put it in simple terms, an interface is a definition of methods and properties. Sometimes this is referred to as a ‘contract’. If a class agrees to implement an interface it must implement all of the properties and methods defined.
One difference you’ll see in an interface vs. a class is that there is no code, only the templates, aka contracts, for the properties and methods. You’ll also notice that there is no access level defined. It’s always considered public.
Here’s an example interface:
Public Interface IPerson Property ID() As Integer Property FirstName() As String Property LastName() As String End Interface
This means that if a class implements the IPerson interface it will have to implement these routines. Here’s an example of a class implementing this interface:
Public Class Customer Implements IPerson Protected _id As Integer Protected _firstName As String Protected _middleName As String Protected _lastName As String Public Property ID() As Integer Implements IPerson.ID Get Return _id End Get Set(ByVal value As Integer) _id = value End Set End Property Public Property FirstName() As String Implements IPerson.FirstName Get Return _firstName End Get Set(ByVal value As String) _firstName = value End Set End Property
Public Property MiddleName() As String Get Return _middleName End Get Set(ByVal value As String) _middleName = value End Set End Property Public Property LastName() As String Implements IPerson.LastName Get Return _lastName End Get Set(ByVal value As String) _lastName = value End Set End PropertyEnd Class
As you can see, each of the properties above, except for MiddleName, has an Implements statement that defines how it will fulfill the Interface contract. The rule is that you can add to the interface inside the implementing class but you can’t take away.
Remember that implementing an interface isn’t the same as creating an instance of a class or creating a subclass. All you’re doing is defining how the external interface should look, not what goes on inside. People do get confused over that point.
Interfaces can be very simple or complex. Most current OOP thought suggests that interfaces be limited to a single method. For example, you might have an IUpdate interface that defines a single Update function and you might have another interface called ICreate that defines a single Create function. You would then combine them into classes that needed to implement them, as in this example:
Public Interface ICreate Function CreateNew(ByVal id As Integer) As Boolean End Interface Public Interface IUpdate Function Update(ByVal id As Integer) As Boolean End Interface
Public Class Customer Implements IPerson, IUpdate, ICreate
As you can see, a class can implement multiple interfaces. Whether it’s better to design with a lot of different interfaces or to group them into logical sections is really up to your own needs. My own take is a bit of a compromise in that I keep them simple with just a few tightly related methods or properties but I don’t religiously limit each interface to a single item.
Another use for interfaces is in defining a common interface for a plug-in architecture as I mentioned in this previous article: How To Create Application Plug-ins In VB.NET. In this case, you will want to have a more complex interface design in most cases since your main app will need a stronger connection to the plug-in.
Do you have any questions about using Interfaces in VB.NET? Please feel free to leave a comment and ask about it.
Entry Filed under: VB.NET Tutorials
Rate This Article: