.NET
  Home arrow .NET arrow Delving Deeper into Interface-Based Progra...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
.NET

Delving Deeper into Interface-Based Programming
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2007-05-31

    Table of Contents:
  • Delving Deeper into Interface-Based Programming
  • Interfaces and Partial Types
  • Implementing Multiple Interfaces
  • Interfaces and Class Hierarchies

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Delving Deeper into Interface-Based Programming


    (Page 1 of 4 )

    In this second article in a four part series, you'll learn about interface methods, properties, and events; how to implement multiple interfaces; and more. It is excerpted from chapter three of Programming .NET Components, Second Edition, written by Juval Lowy (O'Reilly, 2006; ISBN: 0596007620). Copyright © 2006 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

    Interface Methods, Properties, and Events

    An interface isn’t limited only to defining methods. An interface can also define properties, indexers, and events. Example 3-7 shows the syntax for defining all of these in an interface and the corresponding implementation.

    Example 3-7.  An interface can define methods, properties, indexers, and events

    public delegate void NumberChangedEventHandler(int number);

    public interface IMyInterface
    {
     
    void Method1(); //A method
     
    int SomeProperty{ get; set; }//A property
     
    int this[int index]{ get; set;}//An indexer
     
    event NumberChangedEventHandler NumberChanged;//An event
    }

    public class MyClass : IMyInterface
    {
     
    public event NumberChangedEventHandler NumberChanged;

      public void Method1()
     
    {...}
     
    public int SomeProperty
     
    {
       
    get
       
    {...}
       
    set
       
    {...}
     
    }
     
    public int this[int index]
     
    {
       
    get
       
    {...}
       
    set
       
    {...}
     
    }
    }

    Interfaces and Structs

    An interesting use of interfaces with properties involves structs. In .NET, a struct (a Structure in Visual Basic 2005) can’t have a base struct or a base class, because it’s a value type. However, .NET does permit structs to implement one or more interfaces. The reason for this is that sometimes you want to define abstract data storage, and there are a number of possible implementations for the actual structure. By defining an interface (preferably with properties only, but it can have methods as well), you can pass around the interface instead of the actual struct and gain the benefits of polymorphism, even though structs aren’t allowed to derive from a common base struct. Example 3-8 demonstrates the use of an interface (with properties only) as a base type for structs.

    Example 3-8.  Using an interface as a base type for structs

    public interface IMyBaseStruct
    {
      int    SomeNumber{ get; set; }
      string SomeString{ get; set; }
    }

    struct MyStruct : IMyBaseStruct
    {
      public int SomeNumber
      { get{...} set{...} }
      public string SomeString
      { get{...} set{...} }
      //Rest of the implementation
    }
    struct MyOtherStruct : IMyBaseStruct
    {
     
    public int SomeNumber
      { get{...} set{...} }
      public string SomeString
      { get{...} set{...} }
      //Rest of the implementation
    }
    //A method that accepts a struct, without knowing exactly the type
    public void DoWork(IMyBaseStruct storage)
    {...}

    More .NET Articles
    More By O'Reilly Media


       · This article is an excerpt from the book "Programming .NET Components, Second...
     

    Buy this book now. This article is excerpted from chapter three of Programming .NET Components, Second Edition, written by Juval Lowy (O'Reilly, 2006; ISBN: 0596007620). Check it out today at your favorite bookstore. Buy this book now.

    .NET ARTICLES

    - Building Applications with Windows Workflow ...
    - Building the Data and Business Layers Using ...
    - The Transformed XML Explorer in MFC
    - List Control and Property Grid with the MFC ...
    - Font, Shell and Masked Edit Controls for MFC
    - Color, Link and Image Editor Controls for M...
    - New Controls for MFC
    - The Windows Ribbon Framework
    - Markup Language for the Ribbon Framework
    - Visually Upgrade Your MFC Project
    - New Features for the Statusbar in MFC
    - Working with the Statusbar in MFC
    - Iron Speed Design v60 Review
    - Binary and XML Serialization
    - Using CrystalReportViewer to Display Crystal...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek