Net Interfaces

Posted on 2008-11-15
Medium Priority
Last Modified: 2013-11-07
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?
Question by:vaughnwhitehead
LVL 14

Accepted Solution

jjardine earned 600 total points
ID: 22969446
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.

Author Comment

ID: 22969584
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



Assisted Solution

psdavis earned 100 total points
ID: 22969649
You are correct.
So your code might look something like (sorry for the C#)
Person person = obj as Person;
return this.CompareTo( person );
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

LVL 26

Assisted Solution

by:Anurag Thakur
Anurag Thakur earned 300 total points
ID: 22970265
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
LVL 14

Assisted Solution

jjardine earned 600 total points
ID: 22970504
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.  

Author Closing Comment

ID: 31517195
Thanks to all; the light came on

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Loops Section Overview
Suggested Courses
Course of the Month14 days, 7 hours left to enroll

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question