The Delphi Language, Part 1 - Classes and Objects
(Page 10 of 10 )
A class is a value type that can contain data, properties, methods, and operators. Delphi's object model is discussed in much greater detail later in the "Using Delphi Objects" section of this chapter, so this section covers just the basic syntax of Delphi classes. A class is defined as follows:
Type
TChildObject = class(TParentObject)
public
SomeVar: Integer;
procedure SomeProc;
end; This declaration is equivalent to the following C# declaration:
public class TChildObject: TParentObject
{
public int SomeVar;
public void SomeProc() {};
} Methods are defined in the same way as normal procedures and functions (which are discussed in the section "Procedures and Functions"), with the addition of the classname and the dot symbol:
procedure TChildObject.SomeProc;
begin
{ procedure code goes here }
end; Delphi's . symbol is similar in functionality to C# and Visual Basic .NET's . operator when referencing members.
Type Aliases The Delphi language has the capability to create new names, or aliases, for types that are already defined. For example, if you want to create a new name for an Integer called MyReallyNiftyInteger, you could do so using the following code:
type
MyReallyNiftyInteger = Integer; The newly defined type alias is compatible in all ways with the type for which it's an alias, meaning, in this case, that you could use MyReallyNiftyInteger anywhere in which you could use Integer.
It's possible, however, to define strongly-typed aliases that are considered new, unique types by the compiler. To do this, use the type reserved word in the following manner:
type
MyOtherNeatInteger = type Integer; Using this syntax, the MyOtherNeatInteger type will be converted to an Integer when necessary for purposes of assignment, but MyOtherNeatInteger will not be compatible with Integer when used in var and out parameters. Therefore, the following code is syntactically correct:
var
MONI: MyOtherNeatInteger;
I: Integer;
begin
I := 1;
MONI := I; On the other hand, the following code will not compile:
procedure Goon(var Value: Integer);
begin
// some code
end;
var
M: MyOtherNeatInteger;
begin
M := 29;
Goon(M); // Error: M is not var compatible with Integer
In addition to these compiler-enforced type compatibility issues, the compiler also generates runtime type information for strongly-typed aliases. This enables you to create unique property editors for simple types, as you'll learn in Chapter 8, "Mono—A Cross Platform .NET Project."
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. |
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |