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:
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. |
Next: Methods >>
More .NET Articles
More By Xavier Pacheco