Wednesday, June 17, 2009

Simplify Interface Implementation with Visual Studio

Interface types are a key aspect of many .NET projects. Even if you don’t directly build a custom interface, it is very common to implement standard interfaces to ensure your classes and structures integrate within the fabric of the base class libraries.

I’d guess most of you are aware that interfaces by themselves are nothing more than a named set of abstract members (methods, properties and/or events). By defining an interface, you have created a “contract” which provides a polymorphic interface to the caller. Essentially when callers see that you support an interface type, they can rest assured that the object supports a canned set of members.

Given that implementing an interface is an all or nothing proposition, one slightly annoying aspect of working with these types is that the supporting class (or structure) must content with *all* of the members. Thus, if the interface in question contains 10 members, you must ensure each member has the correct arguments, return values, and member names.

Visual Studio has a few tricks which can simply the task of dealing with interfaces. Some of you have (hopefully) been using these features for quite some time, however when I teach .NET courses, I am surprised how many developers are unaware of these features of the IDE. So, in this (real) quick blog post, I’d like to point out two useful shortcuts.

Visually Define and Implement Interfaces

When you add a new *.cd (aka, class designer) file to your project, you can drag over an Interface type from your Toolbox, and populate the members using the related Class Details window:

image

Fair enough, however what if you wish to support this interface on a new type? Although a tad unintuitive, your journey begins by selecting the Inheritance icon from the Toolbox (there is no ‘implements’ icon, although this is the better term when dealing with interfaces). Assume you have added a new Class to your designer named MyClass. Click the Inheritance icons, then click on the class which will be implementing the interface. After this point, click on the interface you want to support. At this point, your designer will look something like so:

image

If you were to look at the code behind MyClass, you’ll see that each member of the interface has been accounted for. At this point, you can gut the stub code and provide real functionality:

public class MyClass : ISomeInterface
{
  #region ISomeInterface Members

  public void MethodA()
  {
    throw new NotImplementedException();
  }

  public int MethodB(string arg1, int arg2)
  {
    throw new NotImplementedException();
  }

  #endregion
}
Implement Interfaces using the Integrated Smart Tag

Let’s say you are pounding away some code, and need to support a set of interfaces on a new type. While you could flip to the designer, you can stay fixed within your code window and still get the much appreciated code gen of the visual designer. As soon as you type in an interface name on a class / structures opening definition, you will be able to activate an interface ‘Smart Tag’. To do so, hover your mouse over the interface itself:

image Here, you can choose to implement the interface implicitly (e.g., as a set of normal public members) or explicitly (e.g., hidden from the type instance and only accessible via a casting operation). Once you select an option, you’ll find that Visual Studio will provide stub code for each member of the interface, and any base interfaces which it extends.

That’s it folks. Told you it would be a short post. Just wanted to throw this out there, as my last class had numerous people who had no clue Visual Studio had these helpful tricks. Take care.

4 comments:

Adil said...

Hi,
I am a .Net web developer.
Big fan of your writing. ur books r detailed and simply explained.
In ur book ProC#2008N.Net3.5Plat, Chap9:Page271.This diff btw interface and Abs class is not clear."Only Derived types can support this polymorphic interface".Seems wrong, May b i m, Please explain.

Adil said...

Hi this is me again...

Pls reply... or email me with some explanation. Waiting eagerly.

or let me know how can i reach you through email.

Bill Richards said...

"... interfaces by themselves are nothing more than a named set of abstract members"

... erm hello??!! You know this is not true, and instead should read

"... interfaces by themselves are nothing more than a named set of VIRTUAL members "

Andrew Troelsen said...

Hi Bill.

Well, in the .NET world, interfaces can only contain abstract members. Any member in a .NET interface is implicitly public and abstract. In C#, virtual implies a canned default implementation in a parent class. Interfaces, being contracts, cannot define semantics, only syntax.

So, this is an error:

public interface IFoo
{
// Error! Can't define virtual
// members in a C# interface.
virtual void SomeMethodInInterface()
{
Console.WriteLine("Default impl.");
}
}

So, I am guessing you are looking at interfaces from a C(++) point of view?

Post a Comment

Note: Only a member of this blog may post a comment.