• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 249
  • Last Modified:

Net Interfaces

I understand that interfaces define properties and methods. A class that implements an interface provides the code for the interface, thus different classes can implement the interfaces in unique ways.  My question deals with NET interfaces.  To quote Balena "The .NET framework defines and consumes dozens of different interfaces, and Expert Visual basic developers should learn to use them profitably."  Net has the IComparable interface. So what?   I need to write the code that implements the interfaces.  What's the advantage?
4 Solutions
The advantage is that by implementing the defined interface instead of writing your own functions your classes can take part in other functions automatically.  For example, Say you want to store an array of your class objects.  Maybe the array has a Sort method that uses the IComparable interface.  This would allow any class that implements IComparable to be stored and sorted with out changing the container class.  This is just a simple example of how these interfaces can be useful.  When the developers created the .Net framework, I am sure they wanted a lot of reusable code functionality.  Providing these basic interfaces allow sub classes and new classes to be processed by built in classes even though they do not know anything more about the sub class than that they implement a function called Compare.  As you look more into built in routines, you will see that this is a huge advantage.  

Yes you can still write your own compare function, but it will only be available to other functions you write and not to the framework or possible external components.
vaughnwhiteheadAuthor Commented:
I don't mean to be dense, but I thought that interfaces do not supply any code only the signature for the interface.  Any time an interface is implemented, I must write the code. For example

Public Class Person

    Implements IComparable

Public Function CompareTo(ByVal obj As Object) As Integer Implements System.IComparable.CompareTo

      ----> I need to write the code that implements this function,  correct?

 End Function

End class


You are correct.
So your code might look something like (sorry for the C#)
Person person = obj as Person;
return this.CompareTo( person );
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Anurag ThakurTechnical ManagerCommented:
read about interface more and you will find out that to implement some functionality it will be rather easy to implement an interface rather than to reinvent the wheel agian
Yes, you have to write the code to implement the function, but the framework routines know to look for those functions.  Here is another example that I deal with every day.   I work with Outlook Add-ins.  for a COM outlook add-in to work in Outlook it must implement IDTExtensibility2 interface.  This defines some basic methods like the application startup, shutdown, and a few others.  Sure I could write a com component that has its own defined startup method, but how would Outlook know how to call it.  when Outlook is running it can load in multiple add-ins from any number of third party vendors.  without the interface, it would not be able to call functions from the add-ins.  With the interface, it doesn't matter which add-in it is, when it starts up, it loops through each add-in, casting it to a type (iDTExtensibility2) and then calls the StartUp Method.  If we didn't have an interface defined, there is no way for Outlook to know what method to call on the add-in.   Not sure if that makes any more sense to you.  
vaughnwhiteheadAuthor Commented:
Thanks to all; the light came on
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now