.NET
  Home arrow .NET arrow Page 12 - The Delphi Language, Part 2
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

The Delphi Language, Part 2
By: Xavier Pacheco
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 15
    2004-07-13

    Table of Contents:
  • The Delphi Language, Part 2
  • Out Parameters, Constant Parameters, and Open Array Parameters
  • Scope and Units and Namespaces
  • The uses Clause and Circular Unit References
  • Object-Oriented Programming
  • Using Delphi Objects
  • Methods
  • Class References and Properties
  • Events
  • Visibility Specifiers, Friend Classes and Class Helpers
  • Nested Types, Operator Overloading and Attributes
  • Interfaces
  • Using Interfaces, SEH
  • Exception Classes

  • 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


    The Delphi Language, Part 2 - Interfaces


    (Page 12 of 14 )

    The Delphi language contains native support for interfaces, which, simply put, define a set of functions and procedures that can be used to interact with an object. The definition of a given interface is known to both the implementer and the client of the interface—acting as a contract of sorts for how an interface will be defined and used. A class can implement multiple interfaces, providing multiple known "faces" by which a client can control an object.

    As its name implies, an interface defines only, well, an interface by which object and clients communicate. It's the job of a class that supports an interface to implement each of the interface's functions and procedures.


    Note - Unlike Win32 Delphi, .NET interfaces to not implicitly descend from IInterface or IUnknown. As such, they no longer implement QueryInterface(), _AddRef, or _Release(). Typecasting is now used for type identity, and reference counting is built in to the .NET platform.


    Defining Interfaces

    The syntax for defining an interface is very similar to that of a class. The primary difference is that an interface can optionally be associated with a globally unique identifier (GUID), which is unique to the interface. The following code defines a new interface called IFoo, which implements one method called F1():

    type
      IFoo = interface
        function F1: Integer;
      end;

    Note that GUIDs are not required for .NET interface definitions, but they are required for Win32. Therefore, the use of GUID is only recommended when you need to maintain a multiplatform code base or want to engage in .NET COM Interop to interoperate between .NET and COM.


    Tip - The Delphi IDE will manufacture new GUIDs for your interfaces when you use the Ctrl+Shift+G key combination.


    The following code defines a new interface, IBar, which descends from IFoo:

    type
      IBar = interface(IFoo)
        function F2: Integer; 
      end;

    Implementing Interfaces

    The following bit of code demonstrates how to implement IFoo and IBar in a class called TFooBar:

    type
      TFooBar = class(TObject, IFoo, IBar)
        function F1: Integer;
        function F2: Integer;
      end;

    function TFooBar.F1: Integer; 
    begin
      Result := 0;
    end;

    function TFooBar.F2: Integer;
    begin
      Result := 0;
    end;

    Note that multiple interfaces can be listed after the ancestor class in the first line of the class declaration to implement multiple interfaces. The binding of an interface function to a particular function in the class happens when the compiler matches a method signature in the interface with a matching signature in the class. A compiler error will occur if a class declares that it implements an interface but the class fails to implement one or more of the interface's methods.


    Interface Methods Made Easy - Let's face it, interfaces are great, but all of that typing to implement interface methods in a class can be a bummer! Here's an IDE trick to implement all the interface methods in just a few keystrokes and mouse clicks:

    1. Add the interfaces you want to implement to the class declaration.

    2. Place the cursor somewhere in the class and press the Ctrl+Spacebar keystroke combination to invoke code completion. The yet unimplemented methods show in red in the code completion window.

    3. Select all the red-colored methods in the list by holding down the Shift key and using the keyboard arrow keys or mouse.

    4. Press the Enter key, and the interface methods will be added automatically to the class definition.

    5. Press the Ctrl+Shift+C keystroke combination to complete the implementation portion of the new methods.

    6. Now all that is left to do is fill in the implementation portion of each method!


    If a class implements multiple interfaces that have methods of the same signature, you must alias the same-named methods as shown in the following short example:

    type
      IFoo = interface
        function F1: Integer;
      end;

      IBar = interface 
        function F1: Integer;
      end;

      TFooBar = class(TObject, IFoo, IBar)
        // aliased methods
        function IFoo.F1 = FooF1;
        function IBar.F1 = BarF1;
        // interface methods
        function FooF1: Integer;
        function BarF1: Integer;
      end;

    function TFooBar.FooF1: Integer; 
    begin 
      Result := 0; 
    end;

    function TFooBar.BarF1: Integer; 
    begin 
      Result := 0; 
    end;


    Note - The implements directive from Win32 Delphi is not available in the current version of the .NET Delphi compiler.


    This chapter is from Delphi for .NET Developer's Guide, by Xavier Pacheco (Sams, 2004, ISBN: 0-672-32443-1). Check it out at your favorite bookstore today.

    Buy this book now.

    More .NET Articles
    More By Xavier Pacheco


     

    .NET ARTICLES

    - 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...
    - Creating Summary .Net Crystal Reports
    - More on Commands, Input and the WPF





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    Stay green...Green IT