.NET
  Home arrow .NET arrow Page 11 - 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 - Nested Types, Operator Overloading and Attributes


    (Page 11 of 14 )

    The Delphi language allows for a type clause to appear with a class declaration, effectively nesting types within a class. Such nested types are referenced using a NestedType.OuterType syntax, as shown in the following code example:

    type
      OutClass = class
        procedure SomeProc;

        type
          InClass = class
            procedure SomeOtherProc;
          end;

      end;

      var
        IC: OutClass.InClass;

    Operator Overloading

    The Delphi language supports the overloading of select operators for classes and records. The syntax for overloading an operator is as straightforward as declaring a class method with a specific name and signature. A complete list of overloadable operators can be found in the Delphi online help under the Operator Overloads topic; however, the following code example demonstrates how you might overload the + and operators of a class:

      OverloadsOps = class
      private
        FField: Integer;
      public
        class operator Add(a, b: OverloadsOps): OverloadsOps;
        class operator Subtract(a, b: OverloadsOps):   OverloadsOps;
      end;

    class operator OverloadsOps.Add(a, b: OverloadsOps): OverloadsOps;
    begin
      Result := OverloadsOps.Create;
      Result.FField := a.FField + b.FField;
    end;

    class operator OverloadsOps.Subtract(a, b: OverloadsOps): OverloadsOps;
    begin
      Result := OverloadsOps.Create;
      Result.FField := a.FField - b.FField;
    end;

    Note that the overloaded operators are declared as class operator and take the declaring class as parameters. Because the + and – operators are binary, they also return the declaring class.

    Once declared, the operators can be used in a manner similar to that shown here:

    var
    O1, O2, O3: OverloadsOps;
    begin
      O1 := OverloadsOps.Create;
      O2 := OverloadsOps.Create;
      O3 := O1 + O2;
    end;

    Attributes

    One of the more exciting features of .NET platform is the realization of attribute-based development, which had been on the drawing board of a few different programming languages for the past several years. Attributes provide a means to tie metadata to language elements such as classes, properties, methods, variables, and so on that provide extended information about those elements to their consumers.

    Attributes are declared using square bracket notation before the element to be augmented. For example, the following code demonstrates the use of the DllImport attribute, which signals to .NET that the method should be imported from the specified DLL:

    [DllImport('user32.dll')]
    function MessageBeep(uType : LongWord) : Boolean; external;

    Attributes are used liberally in .NET. For example, the Browsable attribute on a property determines whether it should be displayed in the Object Inspector:

    [Browsable(True)]
    property Foo: string read FFoo write FFoo;

    .NET's attribute system is quite extensible because attributes are implemented as classes. This allows you to extend the attribute system without limit because you can create your own attributes either from scratch or by inheriting from existing attribute classes and use these new attributes in other classes.

    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

    - 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 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek