The Delphi Language, Part 1 - Constants, Operators
(Page 3 of 10 )
Constants in Delphi are defined in a const clause, which behaves similarly to the C#'s const keyword. Here's an example of three constant declarations in C#:
public const float ADecimalNumber = 3.14;
public const int i = 10;
public const String ErrorString = "Danger, Danger, Danger!"; The major difference between C# constants and Delphi language constants is that Delphi, like Visual Basic .NET, doesn't require you to declare the constant's type along with the value in the declaration. The Delphi compiler automatically allocates the appropriate type for the constant based on its value, or, in the case of scalar constants such as Integer, the compiler keeps track of the values as it works, and space never is allocated. Here's an example:
const
ADecimalNumber = 3.14;
i = 10;
ErrorString = 'Danger, Danger, Danger!'; Optionally, you can also specify a constant's type in the declaration. This provides you with full control over how the compiler treats your constants:
const
ADecimalNumber: Double = 3.14;
I: Integer = 10;
ErrorString: string = 'Danger, Danger, Danger!';
The Type Safety of Typed Constant - Typed constants have one distinct advantage over the untyped variety: Untyped constants' lack of strict compile-time typing prevents them from supporting object method calls. For example, the following code is legal:
const
I: Integer = 19710704;
S: string;
begin
S := I.ToString;
Whereas the following code will not compile:
const
I = 19710704;
S: string;
begin
S := I.ToString;
The Delphi language permits the usage of compile-time functions in const and var declarations. These routines include Ord(), Chr(), Trunc(), Round(), High(), Low(), Abs(), Pred(), Succ(), Length(), Odd(), Round(), Trunc(), and SizeOf(). For example, all of the following code is valid:
type
A = array[1..2] of Integer;
const
w: Word = SizeOf(Byte);
var
i: Integer = 8;
j: SmallInt = Ord('a');
L: Longint = Trunc(3.14159);
x: ShortInt = Round(2.71828);
B1: Byte = High(A);
B2: Byte = Low(A);
C: Char = Chr(46);
Caution - For backward compatibility, the compiler provides a switch that enables typed constants to be assignable, just like variables. This switch is accessible via the Compiler page of the Project, Options dialog box, or using the $WRITEABLECONST (or $J) compiler directive. However, this behavior should be officially deemed icky, and it should be avoided by leaving this functionality at its default disabled state.
Note - Like C# and Visual Basic .NET, the Delphi language doesn't have a preprocessor as does the C language. There's no concept of a macro in Delphi and, therefore, no Delphi equivalent for C's #define for constant declaration. Although you can use Delphi's $define compiler directive for conditional compiles similar to C's #define, you cannot use it to define constants. Use const in Delphi where you would use #define to declare a constant in C.
Operators Operators are the symbols in your code that enable you to manipulate all types of data. For example, there are operators for adding, subtracting, multiplying, and dividing numeric data. There are also operators for addressing a particular element of an array. This section explains some of the Delphi operators and their correlation with C# and CLR counterparts.
Assignment Operators Unless you have some experience with Pascal, Delphi's assignment operator might be one of the toughest things to get used to. To assign a value to a variable, use the := operator as you would use the = operator in C# or Visual Basic .NET. Delphi programmers often call this the gets or assignment operator, and the expression
Number1 := 5; is read either "Number1 gets the value 5," or "Number1 is assigned the value 5."
Comparison Operators If you've already programmed in Visual Basic .NET, you should be very comfortable with Delphi's comparison operators because they're virtually identical. These operators are fairly standard throughout programming languages, so they're covered only briefly in this section.
The Delphi language uses the = operator to perform logical comparisons between two expressions or values. Delphi's = operator is analogous to the C# == operator, so a C# expression written as
if (x == y) is written like this in Delphi:
if x = y
Note - Remember that in the Delphi language, the := operator is used to assign a value to a variable, and the = operator compares the values of two operands.
Delphi's "not equal to" operator is <>, and its purpose is identical to C#'s != operator. To determine whether two expressions are not equal, use this code:
if x <> y then DoSomething Logical Operators
Delphi uses the words and and or as logical "and" and "or" operators, whereas C# uses the && and || symbols, respectively, for these operators. The most common use of the and and or operators is as part of an if statement or loop, as demonstrated in the following two examples:
if (Condition 1) and (Condition 2) then
DoSomething;
while (Condition 1) or (Condition 2) do
DoSomething;
Delphi's logical "not" operator is not, which is used to invert a Boolean expression. It's analogous to the C#'s ! operator. It's also often used as a part of if statements, as shown here:
if not (condition) then (do something); // if condition is false then... Table 5.1 provides an easy reference of how Delphi operators map to corresponding C# and Visual Basic .NET operators.
Table 5.1 Assignment, Comparison, and Logical Operators Operator | Delphi | C# | Visual Basic .NET |
Assignment | := | = | = |
Comparison | = | == | = or Is* |
Not equal to | <> | != | <> |
Less than | < | < | < |
Greater than | > | > | > |
Less than or equal to | <= | <= | <= |
Greater than or equal to | >= | >= | >= |
Logical and | and | && | And |
Logical or | or | || | Or |
Logical not | not | ! | Not |
Logical xor | xor | ^ | Xor |
Arithmetic Operators You should already be familiar with most Delphi language arithmetic operators because they're generally similar to those used in most common languages. Table 5.2 illustrates all the Delphi arithmetic operators and their C# and Visual Basic .NET counterparts.
Table 5.2 Arithmetic Operators Operator | Delphi | C# | Visual Basic .NET |
Addition | + | + | + |
Subtraction | - | - | - |
Multiplication | * | * | * |
Floating-point division | / | / | / |
Integer division | div | / | |
Modulus | mod | % | Mod |
Exponent power | None | None | ^ |
You might notice that Delphi and Visual Basic .NET provide different division operators for floating-point and integer math, although this isn't the case for C#. The div operator automatically truncates any remainder when you're dividing two integer expressions.
Note - Remember to use the correct division operator for the types of expressions with which you're working. The Delphi compiler gives you an error if you try to divide two floating-point numbers with the integer div operator or assign to an integer the result of two integers divided with the floating-point / operator, as the following code illustrates:
var
i: Integer;
d: Double;
begin
i := 4 / 3; // This line will cause a compiler error
d := 3.4 div 2.3; // This line also will cause an error
end;
If you must divide two integers using the / operator, you can assign the result to an integer if you convert the expression to an integer using the Trunc() or Round() functions.
Bitwise Operators Bitwise operators enable you to modify individual bits of a given integral variable. Common bitwise operators enable you to shift the bits to the left or right or to perform bitwise "and," "not," "or," and "exclusive or" (xor) operations with two numbers. The Shift-left and Shift-right operators are shl and shr, respectively, and they're much like the C# << and >> operators. The remainder of Delphi's bitwise operators is easy enough to remember: and, not, or, and xor. Table 5.3 lists the bitwise operators.
Table 5.3 Bitwise Operators Operator | Delphi | C# | Visual Basic .NET |
And | and | & | And |
Not | not | ~ | Not |
Or | or | | | Or |
Xor | xor | ^ | Xor |
Shift-left | shl | << | None |
Shift-right | shr | >> | None |
Increment and Decrement Procedures Increment and decrement operators provide a convenient means to add to or subtract from a given integral variable. Delphi doesn't really provide honest-to-gosh increment and decrement operators similar to the C# ++ and -- operators, but Delphi does provide procedures called Inc() and Dec() intended for this very purpose.
You can call Inc() or Dec() with one or two parameters. For example, the following two lines of code increment and decrement variable, respectively, by 1:
Inc(variable); Dec(variable); The following two lines increment or decrement variable by 3:
Inc(variable, 3); Dec(variable, 3); Table 5.4 compares the increment and decrement operators of different languages.
Table 5.4 Increment and Decrement Operators Operator | Delphi | C# | Visual Basic .NET |
Increment | Inc() | ++ | None |
Decrement | Dec() | -- | None |
Note that the C# ++ and –- operators return a value, whereas Inc() and Dec() do not. Delphi's Succ() and Pred() functions do return a value; however, these do not modify the parameter.
Do-and-assign Operators Not present in the Delphi language are handy do-and-assign operators like those found in C#. These operators, such as += and *=, perform an arithmetic operation (in this case, an add and a multiply) before making the assignment. In Delphi, this type of operation must be performed using two separate operators. Therefore, this code in C#:
x += 5; becomes this in Delphi:present in the Delphi language are handy do-and-assign
x := x + 5;
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: Delphi Language Types >>
More .NET Articles
More By Xavier Pacheco