.NET
  Home arrow .NET arrow Page 6 - 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 - Using Delphi Objects


    (Page 6 of 14 )

    Using Delphi Objects 

    As mentioned earlier, classes are entities that can contain both data and code. Objects are created instances of those classes. Delphi classes provide you with all the power of object-oriented programming in offering full support of inheritance, encapsulation, and polymorphism.

    Declaration and Instantiation

    Of course, before using an object, you must have defined it using the class keyword. As described earlier in this chapter, classes are declared in the type section of a unit or program:

    type
      TFooObject = class;

    In addition to a class declaration, you usually also will have a variable of that class type, or instance, declared in the var section:

    var
     
    FooObject: TFooObject;

    You create an instance of an object in Delphi by calling one of its constructors. A constructor is responsible for creating an instance of your object and allocating any memory or initializing any fields necessary so that the object is in a usable state upon exiting the constructor. Delphi objects always have at least one constructor called Create()—although it's possible for an object to have more than one constructor. Depending on the type of object, Create() can take different numbers of parameters. This chapter focuses on the simple case in which Create() takes no parameters.

    Object constructors in the Delphi language aren't called automatically, and it's incumbent on the programmer to call the object constructor. The syntax for calling a constructor is as follows:

    FooObject := TFooObject.Create;

    Notice that the syntax for a constructor call is a bit unique. You're referencing the Create() constructor of the class by the type rather than the instance, as you would with other methods. This might seem odd at first, but it does make sense. FooObject, a variable, is undefined at the time of the call, but the code for TFooObject, a type, is static in memory. A static call to its Create() constructor is therefore totally valid.

    The act of calling a constructor to create an instance of an object is often called instantiation.


    Note - When an object instance is created using the constructor, the CLR will ensure that every field in your object is zero-initialized. You can safely assume that all numbers will be initialized to 0, all objects to nil, all Boolean values to False, and all strings will be empty.


    Destruction

    All .NET classes inherit a method called Finalize(), which can be overridden to perform any necessary class cleanup. Finalize() is called automatically for each instance by the .NET garbage collector. Note however, that there is no guarantee as to when Finalize() will actually be called or even that it will execute in its entirety in certain circumstances. For these reasons, it is not recommended that critical or limited resources—such as large memory buffers, database connections, or operating system handles—be released in the Finalize() method. Instead, Delphi developers should use the Disposable Pattern by overriding the destructor Destroy() of an object to free valuable resources. How to do this is discussed in Chapter 9, "Memory Management and Garbage Collection."

    The Adam of Objects

    You might be asking yourself how all these methods got into your little object. You certainly didn't declare them yourself, right? Right. The methods just discussed actually come from .NET's base System.Object class. Delphi's TObject class is an alias for System. Object. In .NET, all objects are always descendants of TObject regardless of whether they're declared as such. Therefore, the declaration

    type
      TFoo = class
    end;

    is equivalent to the declaration

    type
      TFoo = class(TObject)
    end;

    Fields

    Adding fields to a class is accomplished with syntax very similar to declaring variables in a var block. For example, the following code adds an Integer, string, and Double to class TFoo:

    type
      TFoo = class(TObject)
        I: Integer;
        S: string;
        D: Double;
    end;

    The Delphi language also supports static fields—that is, fields whose data is shared among all instances of a given class. This is accomplished by adding one or more class var blocks to a class declaration. To illustrate, the following code adds three static fields to the TFoo class:

    type
      TFoo = class(TObject)
      I: Integer;
      S: string;
      D: Double;
      class var
        I_Static: Integer;
        S_Static: string;
        D_Static: Double;
    end;

    Note that it is also legal (although syntactically unnecessary) to have a var block in a class definition that defines normal fields.

    The class var block is identical in function to the static keyword in C#. Note that a class var or var block is terminated by any of the following elements:

    • Another class var or var block

    • A property declaration

    • Any method declaration

    • A visibility specifier

    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 6 hosted by Hostway
    Stay green...Green IT