August 06, 2002 12:08 AM

Implement Interface Methods the Easy Way

DevConnections
Rating: (0)

asp:tip

TECHNOLOGIES: VS .NET

LANGUAGE: C#

 

Implement Interface Methods the Easy Way

It can be quick andautomatic with Class View.

 

 

A common task when creating classes in the .NET Frameworkis to implement an interface defined already in the Framework, one defined inyour own code or in a third-party library. To implement an interface, you mustimplement all its methods with the proper name, parameters, and return type inyour class. Coding this in by hand can be tedious and error-prone. Luckily, anice little feature in the Class View can automate this process for you.

 

To see how to do this, start a new C# Class Libraryproject in Visual Studio. The project will start with a class named Class1. Add the interface you want toimplement to the class. I implemented the ISerializableinterface, defined in the System.Runtime.Serializationnamespace.

 

You class definition should now look like this:

 

public class Class1 :

  System.Runtime.Serialization.ISerializable {}

 

Now go to Class View, expand the solution, namespace, andclass in the tree. Under the Class1class in the tree, you'll see a Bases and Interfaces node. If you expand this,you should see the ISerializableinterface because the class now derives from it. Under ISerializable, you'll see its single method, GetObjectData (see Figure 1). All you need to do to implement themethod is to right-click on it in Class View and select Add >Override from the context menu. The method is added to your class as anempty method with all the right parameters and return type. If the interfacehad multiple methods, you would simply repeat this process for each toimplement each one in turn.

 

The bad news is that if you are a VB programmer, thisfeature is not available in Class View. The Class View context menus aredifferent in VB, and unfortunately this item is missing there.

 


Figure 1. By right-clicking on aninterface method for an interface you have derived your class from, you canimplement the interface methods quickly by selecting Add > Override from thecontext menu.

 

Brian Noyes is an independent software consultant andpresident of Software Insight (http://www.softinsight.com).He's an MCSD with more than 11 years of programming, design and engineeringexperience. Brian specializes in architecture, design, and coding ofcutting-edge software systems, and is a technical editor and frequentcontributor to several publications. E-mail him at mailto:brian@softinsight.com.

 

 

 

Add a Comment

There are no comments to display. Be the first one!
You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement




Comments from the DevConnections Community

Join our community of development pros.

Windows problem

I all, I have a problem on my Windows Vista that began afetr the purchase of an external Hard Disk Freecom. A few days afetr the purchase I discon...

Most Recent Posts

GOOGLE LINKS
SPONSORED LINKS
FEATURED LINKS