1. Home
  2. Computing & Technology
  3. Visual Basic

Using Interfaces In Separate Files
A programming example that adds the information Microsoft left out

By Dan Mabbutt, About.com

Jun 9 2007

Larry Hooven, a programmer from Virginia, wrote in with a question about how to implement an interface in VB.NET.

(The buzz is that "Virginia is for lovers." Is that right, Larry?)

Larry wrote, "I have been attempting to wrap my poor grey cranial matter around interfaces in VB.NET 2005. I think I understand the concept that interfaces don't define code but serve as a list of items that will have to be implemented by the calling class. The big question for me is where do I define my interfaces."

Larry then proposed what is probably the classic example of objects: the dog. I think I've read at least half a dozen articles where a dog is used to illustrate objects. (I use a TV in my main tutorial.) But, hey! I like dogs.

I need to qualify this article by noting that I'm not going to go into detail on what "inheritance" and the fine details of the Implements statement and the Implements keyword. There's a more important question to answer and I need to get to that without filling up several pages first.

Larry continued, "Everything I read is the realm of architects and business analysts, so trying to read about it causes my eyes to glaze over and for me to just about lose consciousness after about roughly 10 seconds of it."

I hear you Larry. Much of the time, that's the EGO effect. The "ego" of the guy telling you how much he knows about it makes your Eyes Glaze Over.

But Larry brings up an excellent point. He does a great job of explaining it.

I have defined a class called Mammal (just the easiest one I could come up with). I wish to derive the Mammal class into a Dog class.

In code that's ...

Public Class Dog
   Inherits Mammal
   Implements ICanine

So far so good, I have a Mammal class that defines the basic Mammal, i.e. warm blood, breaths air, may or may not have fur (it occurred to me whales lack fur, but are mammals).

I plan on having my canine interface define the following:

Property CanBark() as Boolean

Sub Bark()

Function BiteDrawsBlood() as Boolean

Etc.

The question is where do I define my interface?

  • Do I define it in the Mammal class after the End Class construct?
  • Do I create a separate file in my project that defines the various interfaces for ICanine, IFeline, IBovine, IPorcine, ICetacean, et al.?
  • Or to throw mud in the water, do I make a separate file in the Mammal DLL and then just reference the DLL?

Or should I just open up my old C++ book and remember to #INCLUDE STDIO.h?

-------------

Well, Larry ... The answers are yes, yes, you can't do that, and Ach! Himmel! Never!!.

Larry's example is just a little bit off the mark because his interface choice, ICanine only works with dogs (implied by the name). A better example would be an interface for, say, all pets, which would include dogs, cats, fish, snakes ... maybe in some cases spiders or even something worse. Then an interface method might be Feed. You gotta feed a pet or it will die! But the way you do it (in other words, the implementation code) will be very different.

The point of an Interface is that it defines a form that must be implemented but it leaves open the way it's done. In other words, an Interface defines the what but not the how. (The "inheritance" part of Larry's example works great, however. You can inherit all the properties of a Mammal and then add unique code for dogs.)

The really good question that Larry brings up is one that all of the texts and web pages I have consulted simply gloss over. Nobody really answers it. And that's how to use an Interface when you have the code in different files. On the next page, I answer it.

Explore Visual Basic
By Category
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Visual Basic
  4. Using VB.NET
  5. Using Interfaces In Separate Files

©2009 About.com, a part of The New York Times Company.

All rights reserved.