TERRYSMITH.NET    Doing Objects in VB.NET and C# - INTERFACES  
             Home              Painting              Photography              Software              Writing
Previous ] [ Index ] [ Next ]   

5

Interfaces

Interfaces in VB.NET are implemented with the Implements keyword:

Public Class Politican
   Implements IFat, ICorruptable, IPig
   'No logic here
End Class

If you're new to VB.NET, note how the Implements ICorruptable is on a line by itself. No, this isn't an example of my perfectionist coding style. The .NET IDE doesn't allow you to put the Implements on the same line as the class definition. The IDE will put a squiggly mark underneath Implements ICorruptable along with a tooltip that says "End of statement expected".

C# is less wordy, in-line compatible, and uses a colon:

public class Politican : IFat, ICorruptable, IPig
{
   // No logic here either
}

This seems simple enough, but it's at this point that Microsoft failed to implement the IKeepItSimple interface. VB.NET and C# both provide ways for you to expose interface methods not only through the interface, but also through the implementing class itself.

Let's start with C#. If you implement an interface method in the "normal" way, the member is callable on both the class and the interface:

public interface ICorruptable
{
   void takeTheMoney();
}

public class Politican : ICorruptable
{
   public void takeTheMoney()
   {
      // Callable from the class and the interface.
   }
}

Note that public is not specified on the interface's takeTheMoney() method. It's public by default, and the .NET compiler prevents you from specifying an access modifier here.

If you want the member to be callable only through the interface, then you have to use this funky fully-qualified-interface-name syntax:

public class Politican : ICorruptable
{
   void ICorruptable.takeTheMoney()
   {
      // Callable through the interface only.
   }
}

No access modifier is specified because it will have the same access level as the member defined by the interface. Once again, you'll get a compilation error if you try to specify it as public.

Things are a bit simpler in VB.NET. Basically, in VB.NET just keep typing the Implements keyword until your code compiles:

Public Interface ICorruptable
   Sub TakeTheMoney()
End Interface

Public Class Politican
   Implements ICorruptable

   Public Sub TakeTheMoney() Implements ICorruptable.TakeTheMoney
      'Callable through class and interface
   End Sub
End Class

Since you probably ignore code comments, let me repeat what the one above said. This member is callable through the class and through the interface. If you change the access modifier for the member implementation in the class from Public to Private, the method will be publicly available when called through the interface (because that's how it's defined in the interface definition) but hidden from callers using a class pointer.

Confused yet? Don't worry. You can pass this confusion on to the future maintainers of your code by making it even more confusing. Our class member can have a different name than the interface member it implements. Here's a bad example:

Public Interface ICorruptable
   Sub TakeTheMoney()
End Interface

Public Class Politican
   Implements ICorruptable

   Public Sub BuyTheVote() Implements ICorruptable.TakeTheMoney
      'Callers using a class pointer see BuyTheVote().
      'Callers using an ICorruptable pointer see TakeTheMoney().
   End Sub
End Class

Public Sub Main()
   Dim p As New Politican()
   Dim i As ICorruptable

   i = p

   'These call the same code.
   Call i.TakeTheMoney()
   Call p.BuyTheVote()
End Sub
Previous ] [ Index ] [ Next ]   

Amazon Honor System Click Here to Pay Learn More


All images and text on this site are licensed only for viewing on your computer during your visit. No rights to save, copy, print, redistribute, use in derivative works, or in any other manner are allowed or implied without the prior written consent of the author.

All images and text are ©Terry Smith unless otherwise noted. All rights reserved.

terry@terrysmith.net