The Delphi Language, Part 1 - Delphi Language Types
(Page 4 of 10 )
One of the Delphi language's greatest features is that it's strongly typed, making it an excellent fit for the .NET platform. This means that actual variables passed to procedures and functions must be of the same type as the formal parameters identified in the procedure or function definition—very little is done for you in terms of implicit type conversion. Delphi's strongly-typed nature enables it to perform a sanity check of your code—to ensure that you aren't trying to put a square peg in a round hole. After all, the easiest bug fixes are the ones the compiler tells you to perform!
Objects, Objects Everywhere! One of the most important notions regarding fundamental types in the .NET version of the Delphi compiler is that they are all value types capable of implicit conversion to classes. This conversation between value and object and back again is known as boxing and unboxing in .NET parlance. Integers, strings, floating points, and all of the rest are not implemented as primitive concepts in the compiler, as in Win32, but instead map to value types provided either by the .NET Framework or by Borland's RTL or VCL. Unlike Win32 Delphi, these value types can have their own procedures and functions in addition to those available in corresponding classes created by the implicit boxing and unboxing of these types. This allows for a kind of syntax that might be foreign for those coming from native compilers (although perhaps familiar for those experienced with languages such as Java or SmallTalk):
var
S: string;
I: Integer;
begin
I := 42;
S := I.ToString; // can call a method on an Integer!
end;
Note - The string type is used heavily in development. This is not to say that other types are not, but there are some special considerations when using strings. Chapter 11 addresses strings specifically; therefore, you will not see detailed coverage of them here.
A Comparison of Types Delphi surfaces most of the primitive types available in the CLR. Table 5.5 compares and contrasts the base types of the Delphi language with those of C# and the CLR. This table also indicates whether each type is compliant with the CLS.
Table 5.5 A Delphi-to-C#-to-CLR Type Comparison Variable range | Delphi | C# | CLR | CLS Compliant? |
8-bit signed integer | ShortInt | sbyte | System.SByte | No |
8-bit unsigned integer | Byte | byte | System.Byte | Yes |
16-bit signed integer | SmallInt | short | System.Int16 | Yes |
16-bit unsigned integer | Word | ushort | System.UInt16 | No |
32-bit signed integer | Integer | int | System.Int32 | Yes |
32-bit unsigned integer | Cardinal | uint | System.UInt32 | No |
64-bit signed integer | Int64 | long | System.Int64 | Yes |
64-bit unsigned integer | UInt64 | ulong | System.UInt64 | No |
single precision float | Single | float | System.Single | Yes |
double precision float | Double | double | System.Double | Yes |
fixed-point decimal | None | decimal | System.Decimal | Yes |
Delphi fixed-point decimal | Currency | None | None | No |
date/time | TDateTime* | None | System.DateTime | Yes |
variant | Variant, OleVariant | None | None | No |
1-byte character | AnsiChar | None | None | No |
2-byte character | Char, WideChar | char | System.Char | Yes |
Fixed-length byte string | ShortString | None | None | No |
Dynamic 1-byte string | AnsiString | None | None | No |
Dynamic 2-byte string | string, WideString | string | System.String | Yes |
Boolean | Boolean | bool | System.Boolean | None |
*TDateTime is a record that wraps a System.DateTime with methods and operator overloads that makes it behave very much like System.DateTime , but with additional behavior to be compatible with Win32 Delphi's TDateTime type.
Characters
Delphi provides three character types:
WideChar—This CLS-compatible character is two bytes in size and represents a Unicode character.
Char—This is an alias for WideChar. In Win32 versions of Delphi, Char was an AnsiChar.
AnsiChar—An old school one-byte ANSI character.
Never assume the size of a Char (or any other type, for that matter), in your code. Instead, you should use the SizeOf() function where appropriate.
Note - The SizeOf() standard procedure returns the size, in bytes, of a type or variable.
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: Variant Types >>
More .NET Articles
More By Xavier Pacheco