.NET
  Home arrow .NET arrow Page 8 - 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  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
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 / 14
    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 - Class References and Properties


    (Page 8 of 14 )

    Although normal class variables hold a reference to an object, class references provide a means to hold a reference to a class type. Using a class reference, you call class or static methods of a class or create instances of the class. The following code illustrates the syntax for declaring a new class called SomeClass and a class reference type for SomeClass:

    type
      SomeClass = class
        constructor Create; virtual;
        class procedure Foo;
      end;

      SomeClassRef = class of SomeClass;

    Using these types, you could call the SomeClass.Foo() class method through the SomeClassRef class reference type like this:

    var
      SCRef: SomeClassRef; 
    begin
      SCRef := SomeClass;
      SCRef.Foo;

    Similarly, an instance of SomeClass can be created from the class reference:

    var
      SCRef: SomeClassRef;
      SC: SomeClass;
    begin
      SCRef := SomeClass;
      SC := SCRef.Create;

    Note that creating a class via a class reference requires that the class have at least one virtual constructor. Virtual constructors are a feature somewhat unique to the Delphi language, allowing classes to be created by class references, where the specific type of class is not known at compile time.

    Properties

    It might help to think of properties as special accessor fields that enable you to modify data and execute code contained within your class. For components, properties are those things that show up in the Object Inspector window when published. The following example illustrates a simplified Object with a property:

    TMyObject = class
    private
      SomeValue: Integer;
      procedure SetSomeValue(AValue: Integer);
    public
      property Value: Integer read SomeValue write SetSomeValue;
    end;

    procedure TMyObject.SetSomeValue(AValue: Integer);
    begin
      if SomeValue <> AValue then
        SomeValue := AValue;
    end;

    TMyObject is an object that contains the following: one field (an integer called SomeValue), one method (a procedure called SetSomeValue), and one property called Value. The sole purpose of the SetSomeValue procedure is to set the value of the SomeValue field. The Value property doesn't actually contain any data. Value is an accessor for the SomeValue field; when you ask Value what number it contains, it reads the value from SomeValue. When you attempt to set the value of the Value property, Value calls SetSomeValue to modify the value of SomeValue. This is useful for a few reasons: First, it allows the developer to create natural side effects of getting or setting a property (such as recalculating an equation or repainting a control). Second, it allows you to present the users of the class with a simple variable without burdening them with the class's implementation details. Finally, you can allow the users to override accessor methods in descendant classes for polymorphic behavior.

    Like static fields and methods, the Delphi language also supports static properties using the class keyword. The following code shows a class with a static property that accesses a static field:

    TMyClass = class(TObject)
      class var FValue: Integer;
      class procedure SetValue(Value: Integer); static;
      class property Value: Integer read FValue write SetValue;
    end;

    Note that static properties can employ only static fields and methods as getter and setters.

    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

    - Using CrystalReportViewer to Display Crystal...
    - Creating Summary .Net Crystal Reports
    - More on Commands, Input and the WPF
    - Grouping and Aggregating When Querying LINQ ...
    - Commands, Input and the WPF
    - Keyboard and Ink Input with WPF
    - Mouse Input and the WPF
    - Input with Windows Presentation Foundation
    - Introducing LINQ with XML and Databases
    - An Introduction to LINQ
    - Querying LINQ to SQL: Basics
    - Completing a Simple Storefront with LINQ
    - Knowing Your Environment: the System.Environ...
    - Creating the Home Page for a Simple Storefro...
    - LINQ Quickly with Language Integrated Queries

     
    Best Practices for Windows Vista Migration Presentation
    Dell and Microsoft recently held a series of face-to-face seminars entitled, &qu....

     
    Creating a Culture for Code Reuse
    If you oversee development teams you know that like it or not proprietary and ex....

     
    Keys to Web Application Acceleration: Advances in Delivery Systems
    Accelerate Web apps by up to 5x. Ensure significantly faster access to the Web a....

     
    Optimizing Application Monitoring
    Tired of finding out from your customers that you're offline? This white paper e....

     
    Solaris to Solaris Migration -- Migrating applications from Sun SPARC to Dell PowerEdge R900
    This comprehensive Migration Guide reviews the approach that Principled Technolo....

     




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