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