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