Net Interfaces

Posted on 2008-11-15
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

    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

    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


    LVL 7

    Assisted Solution

    You are correct.
    So your code might look something like (sorry for the C#)
    Person person = obj as Person;
    return this.CompareTo( person );
    LVL 26

    Assisted Solution

    by:Anurag Thakur
    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

    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

    Thanks to all; the light came on

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
    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…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    759 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

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now